I am starting this Tutorial series to help the newbies at http://jotil21.net who are very much interested at sharpening their PHP skills. This would enrich my knowledge as well, I believe.
Starting PHP Blocks: “<?php” starts a php block and “?>” ends one. Whenever the PHP engine hits this opening tag, it considers the rest of the contents within the block to be PHP code and parses the code. To simplify the concept — PHP carries out any commands given inside the “<?php ?>” tag.
Mixing With HTML: You can mix up HTML and PHP since both works on tags. Just open up a small php block inside your HTML code to add some interactivity.
<html> <head><title>Title</title> </head> <body> It's <?php echo gmstrftime("%c",(time()+6*60*60)); ?> BST (Bangladesh Standard Time) </body> </html>
Here we used a php block to print out the current BST inside the body tag. More details are covered in the future posts.
Printing Data: If you want to print out some data (text/number or anything), use the “echo” statement.
Example:
<?php echo "Hello, you printed me"; ?>
Please notice a few things:
- We put the text inside double quotes (“”).
- We could use single quotes (”) instead.
- We ended the command with a semi colon (;). It’s a must. If you don’t put the semi colon, you’ll get an error message. It’s because php considers the semicolon to be the end of a statement. If it can’t find a semicolon, it’ll consider that the statement has not ended and continue parsing the next statement as a part of the current statement.
Using “print” function: We can replace “echo” with “print”. Here’s an example:
<?php print "Hello, you like the print method?"; ?>
Alternative Use: Both “echo” and “print” can be used by appending the data inside parentheses. Example:
<?php echo("Hello, parentheses!"); ?> <?php print("Hello, parentheses!"); ?>
Differences: Now, is print() and echo() really the same ? The answer is “no”. What’s the differences then ? Well, let’s see:
1. Speed. There is a difference between the two, but speed-wise it
should be irrelevant which one you use. echo is marginally faster
since
it doesn’t set a return value if you really want to get down to the
nitty gritty.
2. Expression. print() behaves like a function in that you can do:
$ret = print “Hello World”; And $ret will be 1. That means that print
can be used as part of a more complex expression where echo cannot.
print is also part of the precedence table which it needs to be if it
is to be used within a complex expression. It is just about at the bottom
of the precedence list though. Only “,” AND, OR and XOR are lower.
3. Parameter(s). The grammar is: echo expression [, expression[,
expression] … ] But echo ( expression, expression ) is not valid.
This would be valid: echo (“howdy”),(“partner”); the same as: echo
“howdy”,”partner”; (Putting the brackets in that simple example
serves
no purpose since there is no operator precedence issue with a single
term like that.)
So, echo without parentheses can take multiple parameters, which get
concatenated:
echo "and a ", 1, 2, 3; // comma-separated without parentheses echo ("and a 123"); // just one parameter with parentheses
print() can only take one parameter:
print ("and a 123"); print "and a 123";
Well, that’s all for now. Looking forward to writing again very soon .
PHP na shikke palabi kothay???
Vaijaner comment ta WordPress automatically Spam hishebe mark kore rakhsilo…
Karonta sposhto…