PHP Tutorial
PHP Strings
A string is a collection of characters.
A string in PHP can be declared by using single or double quotes.
PHP String Concatenation
String concatenation in PHP differs when single or double quotes are used.
<?php
$name = "John";
//single quote string concatenation
echo 'Hi, my name is '.$name;
//double quote string concatenation
echo "Hi, my name is $name";
//php-strings-example-1
?>
PHP String Functions
PHP comes with built-in functions for handling strings.
Only a few will be listed here. See the PHP Official Documentation for more information.
ucfirst() - Make the first character of a string uppercase
ucfirst() returns a string with its first character capitalized if the string is an alphabetic string.
<?php
$name = "john";
echo ucfirst($name); //outputs: John
?>
substr() - Returns Part of a String
substr() returns part of the given string with offset and length specified.
It takes in three parameters the string, offset and length(optional or null by default) of the output.
None
strtolower() - Converts All Characters of a String to Lowercase
strtolower() returns lowercase of all characters of a string.
<?php
$name = "JOHN";
echo strtolower($name); //outputs: john
//php-strings-example-3
?>
strtoupper() - Converts All Characters of a String to Uppercase
strtoupper() returns uppercase of all characters of a string.
<?php
$name = "john";
echo strtolower($name); //outputs: JOHN
//php-strings-example-4
?>
What You Should Know at the End of This Lesson
- You should know how to concatenate strings.
- You should know the differences between single and double quotes string manipulation.
- You should know to use basic string manipulation functions.