String

  • Strings are sequences of characters.

  • It can be treated as a string assigned to variables, given as input to functions, returned from functions, or sent as output to your user's Web page.

  • The way to define a string in PHP code is to enclose it in quotes, whether single quotes (') or double quotes (").
    Example :
    $name = 'jtechies Heights'; 
    				
    $name = "jtechies Heights";


String Concatenation Operators

  • PHP provide only one real operator that is thedot (.) or concatenation operator.

  • When operator placed between two string arguments,produces a new string that is the result of putting the two strings together in sequence.
Example :
<?php 
$name1 = "jtechies";
$name2 = "Heights";
print($name1 . "..." . $name2);
?>
Output :
jtechies...Heights


String Functions

There are different types of String Functions :

strlen()
strlen() is use to find out length of string or string length.
Example :
<?php
echo strlen("jtechies heights!");
?>
Output :
17
					

String Replacement
str_replace() use to replace all instances of a particular substring with an alternate string.
Example :
<?php
$first= "jtechies is a free tutorial website.";
print($first);
$second = str_replace("jtechies", "jtechies Heights",$first);
print($second);
?>
Output :
jtechies is a free tutorial website.
jtechies Heights is a free tutorial website.

String Search
  • strstr() function use to take a string to search in and a string to look for.

  • If it succeeds, it returns the portion of the string that starts with the first instance of the string it is looking for.
  • Example :
    <?php
    $string_to_search = "jtechies Heights";
    $string_to_find = "jtechies";
    print("Result of looking for $string_to_find" .
    strstr($string_to_search, $string_to_find) . "
    "); $string_to_find = "jtechiesHeights"; print("Result of looking for $string_to_find" . strstr($string_to_search, $string_to_find)); ?>
    Output :
    Result of looking for jtechiesjtechies Heights
    Result of looking for jtechiesHeights
    						

    Case functions

    • Case functions use to change lowercase to uppercase and vice versa.

    strtolower()
    • strtolower() use to returns an all-lowercase string.

    • It doesn't create problem if the original is all uppercase or mixed.
    Example :
    <?php
    $low = "jtechies HeiGhts";
    print($low);
    $lower = strtolower($low);
    echo $lower;
    ?>
    
    Output :
    jtechies HeiGhts
    jtechies heights
    
    


    strtoupper()
    • strtoupper() use to returns an all-uppercase string.

    • It doesn't create problem if the original is all uppercase or mixed.
    <?php
    $upp = "www.tkhts.com";
    echo($upp);
    $upper=strtoupper($upp);
    echo($upper);
    ?>
    
    Output :
    www.tkhts.com
    WWW.TKHTS.COM