Strings

Display String

$technology = "PHP";
echo $technology;
PHP

$technology = "PHP";
print $technology;
PHP

$technology = "PHP";
<?= $technology ?>
PHP

Concatenation

$action = "Learn";
$technology = "PHP";
echo $action . $technology;
Learn PHP

$action = "Learn";
$technology = "PHP";
echo "$action $technology";
Learn PHP

$technology =  "PHP";
$version = 7.3;
$web_based = true;
echo "Technology: " . $technology . ", Version: " , $version . ", Web: " . $web_based;
Technology: PHP, Version: 7.3, Web: 1

heredoc (for documentation)

$dev = "Web Development";
$amount = 10;

// just prints whatever is written inside the block, no operation is performs except variable substitution.
echo <<< DOC_DEV
"PHP" seems very simple language. Very cool language for $dev. ($amount * 10)
DOC_DEV 
"PHP" seems very simple language. Very cool language for Web Development. (10 * 10)

nowdoc (for documentation - write the label in single quotes)

$dev = "Web Development";
$amount = 10;

// just prints whatever is written inside the block, no variable substitution.
echo <<< 'DOC_DEV'
"PHP" seems very simple language. Very cool language for $dev. ($amount * 10)
DOC_DEV 
"PHP" seems very simple language. Very cool language for $dev. ($amount * 10)

String Length

$language = "PHP";
$subject = "  ";
$teacher = "";
echo strlen($language);
echo strlen($subject);
echo strlen($teacher);
echo strlen($class); // variable is not defined, the strlen will return 0
3
2
0
0

if (strlen($fullName) > 0) {
      echo $fullName;
}
else {
      echo "not defined, empty or null";
}
not defined, empty or null

String Position

// strpos() search is case sensitive
$subject = "Welcome to the Learning PHP Website";

$stringToFind = "W";
$stringPosition = strpos($subject, $stringToFind);
echo $stringPosition;

$stringToFind = "P";
$stringPosition = strpos($subject, $stringToFind);
echo $stringPosition;

$stringToFind = "website";
$stringPosition = strpos($subject, $stringToFind);
echo $stringPosition; // this will not output anything due to case sensitivity. The letter W is in lower case. It is false.
0   Note:  The string position of the first character is 0
24

// stripos() search is case insensative
$subject = "Welcome to the Learning PHP Website";
$stringToFind = "LEARNING";
$stringPosition = stripos($subject, $stringToFind);
echo $stringPosition;
15

Word Count

$suject = "Welcome to the Learning PHP Website";
echo "Total words: " . str_word_count($subject);
Total words: 6

Replace String

$suject = "Welcome to the Learning PHP Website";
echo str_replace("Welcome", "Hello and welcome", $subject);
Hello and welcome to the Learning PHP Website

Reverse String

$suject = "Mars";
echo strrev($subject);
sraM

Shuffle String

$suject = "Welcome to the Learning PHP Website";
echo str_shuffle($subject);
boonrtseWth emW HgncP eee tiLeliPa

Uppercase & Lowercase

$suject = "Welcome to the Learning PHP Website";
echo strtoupper($subject);
echo strtolower($subject);
WELCOME TO THE LEARNING PHP WEBSITE
welcome to the learning php website

Wordwrap

$suject = "Welcome to the Learning PHP Website";
echo wordwrap($subject, 5, "<br />", true); // true menas, break the word if the length reaches to 5
Welco
me to
the
Learn
ing
PHP
Websi
te

$suject = "Welcome to the Learning PHP Website";
echo wordwrap($subject, 20, "<br />", false); // false menas, do not break the word if the length reaches to 20
Welcome to the
Learning PHP Website

String Trim

$suject = " Welcome to the Learning PHP Website  ";
echo strlen($subject);
echo strlen(ltrim($subject));
echo strlen(rtrim($subject));
echo strlen(trim($subject));
38
37
36
35