Related post

PHP Strings - How to create and manipulate Strings in PHP - Zhullyblog




Hello, welcome to zhullyblog. If this is your first time here , please Subscribe to Zhullyblog by Email to get latest tutorial.


PHP strings
What is a string?

A string is a sequence of letters ,numbers, special characters or the combination of characters - letters and numbers.

 Every programming language deals with a string. So in this Tutorial, we would learn to create , concatenate and basically manipulate a string.

How to create a string in PHP
In PHP, you create a string by enclosing it in a single or double quotation marks. Although, the single and double quotation mark works in different ways.

Let’s begin in full.
statement.






$example = ' Hello World ';
 
That is an example of a string.
Here is the PHP code
statement.




<?php
$str = 'Hello world';
echo  $str;
?>





Manipulate strings
Calculating the length of a string
To calculate the length of a string, you use the strlen( ) function. This means that you use the function to get the number of characters in a string with the blank space inclusive.

Here is an example
statement.





<?php
$str = 'Welcome to Zhullyblog';
echo strlen($str);
?>




Reverse a string
The strrev ( ) function is used to reverse a string.

Here is an example
statement.





<?php
$sample = "Welcome to zhullyblog";
echo strrev($sample);
?>




Replace text within a string
The str_replace( ) is used to replace text in a string.
Here is an example

statement.




<?php
$sample = "Welcome to blog";
echo str_replace("zhullyblog" ,"blog" ,$sample);
?>




Count number of words in a string
The str_word_count( ) function is used to count the number of words in a string.
Here is an example
statement.




<?php
$sample = "Welcome to blog";
echo str_word_count($sample);
?>




Please share





Follow us on




OTHER TUTORIALS


Other Tutorials










Comments