QUICK INTRO TO PHP STRINGSPDF format:
Quick_Into_to_PHP_Strings.pdf ( 344.39k )
Number of downloads: 32
Title: Quick Intro to PHP Strings
Audience: Beginner
This tutorial is aimed at PHP beginners, or for people that just want to remind about strings in PHP.Unlike C and Some other languages Strings in PHP are not array of characters. Strings are of type scalar. Type scalar are boolean, integer, double, and string. They can hold only one value at a time.
The three ways to declare/define a string
A string literal can be specified in four different ways:
1. single quoted
2. double quoted
3. heredoc syntax
4. nowdoc syntax (since PHP 5.3.0) “I just learned about this one while I was writing this tutorial”
CODE
$text = 'Hello World';
$text = "Oh no!\n Not Hello World again.";
$text = <<<GO
Hello again. This way I can write on and on using “double quote” or ‘single quote’ and as you can see its being treated just as we intended
GO;
STRINGS IN DOUBLE QUOTE ARE INTERPOLATED. THIS MEANS THAT IF YOU PUT A VARIABLE INSIDE IT, IT WILL RETURN THE VALUE OF THAT VARIABLE.Example:
CODE
$text = 'dreamincode';
echo "$text is the best";
WAIT BUT THERE IS A CATCH. IF THE VARIABLE IS AN ARRAY YOU WILL NEED TO HELP PHP WITH THE DECISION.
Example:
CODE
$month = array( "April", "May", "June" );
echo "the month is $month[ 0 ]";
The code above will not display the value. Instead it will print the string as you see it.
To print the value the way we intended we will have to put the array inside a bracket { } or we can use the dot (.) operator to concatenate strings.
Example:
CODE
echo "The month is { $month[ 0 ] }";
echo "The month is " . $month[ 1 ];
ALSO, IF YOU USE ESCAPE CHARACTER IN DOUBLE QUOTE STRING IT WILL DO JUST WHAT WE INTEND,
WHICH IS PERFORM ITS TASK LIKE ( \N, \T, \\, \$). BUT IF WE USE ESCAPE CHARACTER IN A SINGLE QUOTE STRING ALL WE GET IS THE TEXT AS IT WAS ASSIGN.Example: Check how these two identical strings are interpreted differently.
CODE
echo "This is a string. \n I just jumped to a new line.";
Output:
This is a string.
I just jumped to a newline.
CODE
echo 'This is string. \n I just jumped to a new line.';
Output:
This is string. \n I just jumped to a new line.
STRING AS ARRAY PHP have the ability to treat strings as Arrays in some situation.
Example:
CODE
$text = "Rhode Island";
for( $i = 0; $i < strlen( $text ); $i++ )
{
echo $text[ $i ] .'<br/>';
}
The example above will loop through the string and print each character.
FUNCTIONS REFERENCE:nl2br( $text ) --> Converts all newline ( \n or \r ) to <br/>
strlen( $text ) --> return the string length
trim( $text ) --> removes whitespace from both sides
ltrim( $text ) --> removes whitespace from left only
rtrim( $text ) --> removes whitespace from right only
Prints a string. Parentheses are not necessary since they are a constructor of the PHP language.
print( $text )
echo( $text )
str_replace( $search, $replacement, $text ) --> Replace all occurences of the $search string with the $replacement string
str_split( $text ) --> Converts a string to an Array, the second argument $split_length is default to 1
strip_tag( $text ) --> Strip HTML and PHP Tags from string, the second argument $allowable_tags is default to empty
strpos( $text, $search ) --> find the position (index) of the first occurence of $search in $text ($haystack)
strrpos( $text, $search ) --> find the position of the last occurence of the $search string in the $text
strrev( $text ) --> Reverse a string example Hello -> olleH
substr( $text, $start ) --> Return part of a string, the third argument is default to strlen( $text )
strtolower( $text )
strtoupper( $text )
wordwarp( $text, 100 ) --> Wraps a string to a given number of character, the second and third argument are default to 75 and '\n'
by default the work will not be cutoff if you don't play with the fourth argument.
EVEN MORE FUNCTIONSexplode( ' ', $text ) --> returns an array of the string $text, the first parameter is a delimeter( character, comma, anything!? )
implode( ' ', $text_array ) --> it does exactly what explode does, but it differs in that i gets an array and return a string instead.
htmlentities( $text ) returns the text with HTML entities if there is any special HTML character in the text.
Example:
" -> "
& -> &
< -> <
> -> >
rawurlencode( $text ) --> return text in a url format
rawurldecode( $text ) --> return text in a normal format not url
Author: Joaquim Costa aka Quim
Email: quim88@hotmail.com
Please submit any mistakes so I can correct them.RESOURCES USE FOR THIS TUTORIAL:PHP. (2008, May 25). PHP: Strings Functions - Manual. Retrieved May 25, 2008, from PHP website:
http://us.php.net/stringsPHP. (2008, May 25). PHP: Strings . Retrieved May 25, 2008, from PHP website:
http://us.php.net/types.stringThis post has been edited by quim: 31 May, 2008 - 10:22 PM