Saturday, November 30, 2013

PHP String and diffirent between single quotes and double quotes

PHP - String:
In the last lesson, PHP Echo, we used strings a bit, but didn't talk about them in depth. Throughout your PHP career you will be using strings a great deal, so it is important to have a basic understanding of PHP strings.

PHP - String create double quotes:


Thus far we have created strings using double-quotes, but it is just as correct to create a string using single-quotes, otherwise known as apostrophes.

PHP Code:

$my_string = 'PHP tutorial example - Welcome the first php aplication!';
echo 'PHP tutorial example - Hello PHP.net!';
echo $my_string;

If you want to use a single-quote within the string you have to escape the single-quote with a backslash \ . Like this: \' !
echo 'PHP tutorial example- It\'s Neat!';

PHP - String create double quotes:

PHP Code:
$my_string = "Beginer - PHP tutorial example!";
echo "Developer - PHP employment";
echo $my_string;


We have used double-quotes and will continue to use them as the primary method for forming strings. Double-quotes allow for many special escaped characters to be used that you cannot do with a single-quote string. Once again, a backslash is used to escape a character.

Note: If you try to escape a character that doesn't need to be, such as an apostrophe, then the backslash will show up when you output the string.

These escaped characters are not very useful for outputting to a web page because HTML ignore extra white space. A tab, newline, and carriage return are all examples of extra (ignorable) white space. However, when writing to a file that may be read by human eyes these escaped characters are a valuable tool!

PHP - String create heredoc
PHP Code:
$my_string = <<<TESTOne of the limitations of strpos is that it only returns the position of the very first match. If there are 5,000 other matches in the string you would be none the wiser, unless you take action!
TEST;

echo $my_string;

Display:
One of the limitations of strpos is that it only returns the position of the very first match. If there are 5,000 other matches in the string you would be none the wiser, unless you take action!

Explain:
There are a few very important things to remember when using heredoc.

Use <<< and some identifier that you choose to begin the heredoc. In this example we chose TEST as our identifier.
Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was TEST;
The closing sequence TEST; must occur on a line by itself and cannot be indented!

Another thing to note is that when you output this multi-line string to a web page, it will not span multiple lines because we did not have any <br /> tags contained inside our string! Here is the output made from the code above.

No comments:

Post a Comment