String variable in php

1,581 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,581
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

String variable in php

  1. 1. Instruction By: Dr. Boueun Chhun 1
  2. 2. Content:1. String Variable2. Concatenate Operators3. String with Single and DoubleQuote4. String with heredoc5. The PHP strlen() function6. The PHP strpos() function7. The str_replace function8. The strtoupper() function9. The ucfirst() function10. The trim() function 211. The strtolower() function
  3. 3. String Variable  String variables are used for values that contain characters.  After we have created a string variable we can manipulate it.  A string can be used directly in a function or it can be stored in a variable.  Here is PHP script with string value <?php $str=‚This is String in PHP.‛; echo $str; ?>  Output of the above script are: This is string in PHP 3
  4. 4. The PHP Concatenation Operator  There is only one string operator in PHP.  The concatenation operator (.) is used to join two string values together.  Below is the example of string concatenation <?php $txt1="Hello world!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2; ?>  The result of the above Hello world What a nice day! 4
  5. 5. String with Single and Doublequote  There are two way when you can specify a string in php single quote and double quote.  Single quoted strings are treated almost literally.  double quoted strings replace variables with their values as well as specially interpreting certain character sequences.  Here is an example <?php $variable=‚name‛; $literally=‘My $variable will not print n’; print($literally); $literally=‚My $variable will print n‛; 5 print($literally);
  6. 6.  The result of the above code will be My $variable will not print!n My name will print String with double quote will process in both two way: 1. Certain character sequences beginning with backslash () are replaced with special characters 2. Variable names (starting with $) are replaced with string representations of their values. The escape-sequence replacements are: n is replaced by the newline character r is replaced by the carriage-return character t is replaced by the tab character $ is replaced by the dollar sign itself ($) " is replaced by a single double-quote (") is replaced by a single backslash () 6
  7. 7. String with Heredoc  You can assign multiple lines to a single string variable using heredoc .  See the example below <?php echo <<<END This uses the "here document" syntax to output multiple lines with variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace! 7 <br />
  8. 8.  In code above after heredoc must be follow by some identifier for example END and must be end with the same identifier as the start one with semiconlon sign Even though using heredoc is difficult and can Couse some problem. Note that the closing sequence END; must occur on a line by itself and cannot be indented. 8
  9. 9. The PHP strlen() Function  Strlen() function used to return the length of the string example: <?php $str=‚Welcome back to PHP‛; echo strlen($str); ?>  Result will be 19  The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop 9 the loop after the last character in the string).
  10. 10. The strops() Function  Strpos() function is used to return the search of character with in the string.  If match is found, function will return the position of the first character of the match word. example: <?php echo strpos(‛Welcome back to PHP‛,‛PHP‛); ?>  The result of the above script is follow 16  The resean is that it return only the first 10 character of the whole word .
  11. 11. The str_replace function  The str_replace function is use to search and replace the search result with the new word.  If no word match it will return the original word. example <?php $str=‚Welcome back to PHP.‛; echo str_replace(‚PHP‛,‛PHP Function‛,$str); ?>  This will search for the word PHP and replace with the word PHP Function.  The result is: 11 Welcome back to PHP Function.
  12. 12. The strtoupper() Function The strtoupper() function is used to convert all lowercase of the string word or sentence to uppercase. example <?php $str=‚start your first php withw3school.‛; echo strtoupper($str); ?> Here is the result START YOUR FIRST PHP WITH W3SCHOOL. 12
  13. 13. The strtolower() Function  The strtolower() function is used to convert all uppercase string to lowercase. example <?php $str=‚START YOUR FIRST PHP WITH W3SCHOOL.‛; echo strtolower($str); ?>  Here is the result start your first php with w3school. 13
  14. 14. The ucfirst() function  Ucfirst() function used to capital the first letter of the first word in the whole string. example <?php $str=‚using ucfirst() function.‛; echo($str); ?>  The result is Using ucfirst() function. 14
  15. 15. The trim() function  Trim() function strip a string of white space in front and at the end of the whole string. example <?php $str=‚ Welcome! ‛; echo($str); ?>  The above will result as below Welcome! 15
  16. 16. Referent:1. http://www.w3schools.com/php/php_string.asp2. http://www.tutorialspoint.com/php/php_variable_types.htm 16

×