Search This Blog

Monday, December 21, 2009

Strings and Comments

What is String?
String comprizes of One or More Characters. A string can be used directly in a function or it can be stored in a variable. We use "echo" or "print" to display the string.


PHP Code:
<?php
echo "Hi This is First String!";
?>


Output:
Hi This is First String!


Concatenating Two Strings: 
The concatenation operator (.)  is used to put two string values together.

Suppose we have two strings lets say string one contain Name and string second contain Surname and if you want to concate these two strings then we need to use the concatenation operator (.) 

PHP Code:
<?php
$name = "Cristiano";
$surname = "Ronaldo";
echo $name." ".$surname;
?>


Output:
Cristiano Ronaldo

Careful when using Quotes
You must be very careful when using quotes in strings! Echo uses quotes to define the beginning and end of the string, so you must use one of the following if your string contains quotations:
  • Don't use quotes inside your string.
  • 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. \"
  • Use single quotes (apostrophes ') for quotes inside your string.
PHP Code:

<?php

// Wrong Method - Gives an Error
echo "Hi I am writing String";  

// Correct Method to write String
//Here we used Quotes
echo "Hi I am writing String";  
 
//or
//Here we used an apostrophe '
echo "Hi I am writing String";  
?>



Comments in PHP:
Comments are very useful in PHP to trace the code in future.
There are two ways to wrie comments in PHP
  • /* Comment                                                                                                                                             Comment                                                                                                                                             Comment                                                                                                                                             Comment */
  • // Comment
(Note: Inline Comment - If you are using this method then you need to write // in each and every line. If you want to comment one or two lines then use // and if you want to comment some particular portion of the php code then use '/* */' )

PHP Code:
<?php
$name = "Cristiano";      // Hi This is Comment
$surname = "Ronaldo";
echo $name." ".$surname;

/* I have used Concatenation Operator to Join Two Strings */

?>


Regards,
Rohit Modi


No comments:

Post a Comment

Website Visitors: blogger visitor