Strings¶
Single Quoted Strings¶
<?php
$currency = 'penny';
$sampleString = 'A $currency saved is a $currency earned.';
echo $sampleString;
echo "<br>";
$misc = 'St. Patrick\'s Day\n';
echo $misc;
//output:
//A $currency saved is a $currency earned.
//St. Patrick's Day\n
?>
Double Quoted Strings¶
<?php
$currency = penny;
$sampleString = "A $currency saved is a $currency earned.";
echo $sampleString."<br>";
$var = 2;
echo "{$var}nd place<br>";
echo $var."nd place<br>";
$misc = "St. Patrick\"s <br>Day\n";
echo $misc;
//output:
//A penny saved is a penny earned.
//2nd place
//2nd place
//St. Patrick"s
//Day
?>
Here Document¶
<?php
echo <<<EOT
Be not afraid of greatness;
some are born great,
some achive greatness,
and others have greatness thrust upon them.
-William Shakespeare
EOT;
?>
Note
Do not put a space before or after EOT
print()¶
<?php
print "without parentheses<br>";
print ("with parentheses<br>");
echo "can", "'t ", "be used ", "with ", "print";
//output:
//without parentheses
//with parentheses
//can't be used with print
?>
Note
last statement can’t be done with print
String built in functions¶
Changing Case¶
<?php $quote = "To be or not to be, that is the question."; $quote = strtolower($quote); echo $quote."<br>" $quote = strtoupper($quote): echo $quote //output: //to be or not to be, that is the question. //TO BE OR NOT TO BE, THAT IS THE QUESTION. ?>
String length¶
<?php
$quote = "To be or not to be, that is the question.";
$length = strlen($quote);
echo $length;
//output: 54
?>
String position¶
<?php
$quote = "Courage is resistance to fear, mastery of fear, no absence of fear.";
echo strpos($quote, "fear")."<br>";
echo strpos($quote, "fear",26)."<br>";
echo strpos($quote, "c")."<br>";
echo strpos($quote, "C")."<br>";
echo strpos($quote, "z");
//output:
//25
//42
//19
//0
?>
Note
- strpos is case sensitive
- If the letter or word does not exist the return is nothing
String Replace¶
<?php
$quote = "To be or not to be, that is the question.";
$replaced = str_replace("be", "know", $quote)."<br>";
echo $replaced;
$replaced = str_replace("know", "be", $replaced, $count);
echo $replaced."number of replaces= ".$count;
//output:
//To know or not to know, that is the question.
//To be or not to be, that is the question.
//number of replaces= 2
?>
Sub Strings¶
<?php
$quote = "Only those who will risk going too far can possibly find out how far one can go.";
echo substr($quote, 3)."<br>";
echo substr($quote, -5)."<br>";
echo substr($quote, 3, 5)."<br>";
echo substr($quote, 3, -5)."<br>";
//output:
//y those who will risk going too far can possibly find out how far one can go.
//n go.
//y tho
//y those who will risk going too far can possibly find out how far one ca
?>
Note
3 is the third character from the beginnig while -5 is the fifth character from the end
String Split¶
<?php
$quote = "Hello world";
$varArray = str_split($quote);
Print_r($varArray);
echo "<br>";
$varArray = str_split($quote, 3);
Print_r($varArray);
//output:
//Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => w [7] => o [8] => r [9] => l [10] => d )
//Array ( [0] => Hel [1] => lo [2] => wor [3] => ld )
?>