saftutorials.com
PHP Strings Tutorial

PHP string is a collection of characters i.e. "Hello John!". A PHP string value must stay with " ".

What is PHP string?
When a value stays within " " then it's called be PHP String. Like "Hello John!"


These are valid example of strings in PHP

<!DOCTYPE html>
<html>
<body>
<?php
$first_string = 'This is a string in single quotes';
$second_string = "This is a string in double quotes";
?>
<p>First String : <?php echo $first_string;?></p></br>
<p>Second String : <?php echo $second_string;?></p></br>
</body>
</html>


The above code produces the following results

Outputs: First String: This is a string in single quotes

Second String: This is a string in double quotes


String Concatenation Example

<!DOCTYPE html>
<html>
<body>
<?php
$first_string = 'This is a string';
$second_string = "This is another string";
?>
<p>Concatenation in PHP : <?php echo $first_string . 'And' .$second_string;?></p>
</body>
</html>


The above code produces the following results

Outputs: Concatenation in PHP: This is a string And This is another string


Some PHP Strings Function Example

<!DOCTYPE html>
<html>
<body>
<?php
$string = 'This is a string';
?>
<p>Word Count : <?php echo str_word_count($string);?></p></br>
<p>String length : <?php echo strlen($string);?></p></br>
<p>String position : <?php echo strpos($string, 'string');?></p></br>
<p>Reverse String : <?php echo strrev($string);?></p></br>
</body>
</html>


The above code produces the following results

Outputs: Word Count: 4

String length: 16

String position: 10

Reverse String: gnirts a si sihT


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic