Monday, August 2, 2010

PHP Comment and PHP echo

PHP Comment:


• Syntax for single line comment-

   o <?php

   o #This is an example comment

   o //This is another example comment

   o echo "Sentence 1."; //This is our first statement

   o echo "<br />"; #This is a line break

   o echo "Sentence 2."; // This is our second statement

   o ?>

• U will notice that both types of commenting styles i.e. ‘//’ and ‘#’ r used on a single line .

• But it is very tedious task when needs to comment multiple lines. So there is another way of commenting. This style is used for multiple lines:

  o <?php

  o /* This is an easier

  o way to write comments

  o on multiple lines */

  o ?>

• So ‘/* and */’ is used for multiple line comments.


PHP-echo


• As u know php command echo is used 4 outputting text 2 a web browser but how to use echo command 4 displaying values stored in variables????

• Observe the following code:

<?php

$myString=”Hello!”;

echo $myString;

echo “<h5>I luv using PHP!!</h5>”;

?>

Display:

Hello!

I luv using PHP!!



• In the above example, two statements include echo. The first 1 simply displays what is contained in $myString variable. The second one uses html and simple string.

• NOTE- html used in php tags should be within quotes.


Careful when echoing quotes:

• It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! Echo uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:

  o Don't use quotes inside your string

  o Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \"

  o Use single quotes (apostrophes) for quotes inside your string.

  o See our example below for the right and wrong use of echo:

• PHP Code:

  o <?php

  o // This won't work because of the quotes around specialH5!

  o echo "<h5 class="specialH5">I love using PHP!</h5>";

  o // OK because we escaped the quotes!

  o echo "<h5 class=\"specialH5\">I love using PHP!</h5>";

  o // OK because we used an apostrophe '

  o echo "<h5 class='specialH5'>I love using PHP!</h5>";

  o ?>

• If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape the quotations by placing a backslash in front of it ( \" ). The backslash will tell PHP that you want the quotation to be used within the string and NOT to be used to end echo's string.



                                                                                                                                      To be continued...

No comments:

Post a Comment