Custom Functions in PHP |Call by Reference and Call by Value Functions in PHP
What is a PHP string?
Sequence of characters is known as string. There are different ways to define a string statement in PHP, they are known as PHP strings. There are different ways to specify a string literal in PHP. The string variables can contain alphanumeric characters.
Single quoted
Let’s now look at the four different ways of creating PHP string functions and string manipulation in PHP.
Creating PHP Strings Using Single quotes: The simplest way to create a string is to use single quotes. Let’s look at an example that creates a simple string in PHP.
<?php $str='Hello text within single quote'; echo $str; var_dump('You need to be logged in to view this page'); //If the single quote is part of the string value, it can be escaped using the backslash. echo 'I \'ll be back after 20 minutes'; ?>
Hello text within single quote
string(42) "You need to be logged in to view this page"
I'll be back after 20 minutes
Double quoted
The double quotes are used to create relatively complex strings compared to single quotes. Variable names can be used inside double quotes and their values will be displayed.
Let’s look at an example.
<?php $str="Hello text within double quote"; echo $str; $name='Alicia'; echo "$name is friends with kalinda"; ?>
Hello text within double quote
Alicia is friends with kalinda
What are PHP string functions?
PHP string functions helps to manipulate strings in different ways.
We are now going to look at some of the commonly used string functions in PHP
Function | Description | Example | Output |
strtolower | Used to convert all string characters to lower case letters | echo strtolower( ‘Amit’); | outputs amit |
strtoupper | Used to convert all string characters to upper case letters | echo strtoupper(‘How are you’); | HOW ARE YOU |
strlen | The string length function is used to count the number of character in a string. Spaces in between characters are also counted | echo strlen(‘united states of america’); | 24 |
explode | Used to convert strings into an array variable | $settings = explode(‘;’, “host=localhost; db=sales; uid=root; pwd=demo”); print_r($settings); | Array ( [0] => host=localhost [1] => db=sales [2] => uid=root [3] => pwd=demo ) |
substr | Used to get sub part of the string | $my_var = ‘This is a really long sentence that I wish to cut short’;echo substr($my_var,0, 12). | This is a re… |
str_replace | Used to locate and replace specified string values in a given string. The function accepts three arguments. The first argument is the text to be replaced, the second argument is the replacement text and the third argument is the text that is analyzed. | echo str_replace (‘the’, ‘this’, ‘the laptop is very expensive’); | this laptop is very expensive |
strpos | Used to locate the and return the position of a character(s) within a string. This function accepts two arguments | echo strpos(‘PHP Programing’,’Prog’); | 5 |
sha1 | Used to calculate the md5 hash of a string value | echo md5(‘password’); | 9f961034ee 4de758 baf4de09ceeb1a75 |
str_word_count | Used to count the number of words in a string. | echo str_word_count (‘This is a really long sentence that I wish to cut short’); | 12 |
ucfirst | Make the first character of a string value upper case | echo ucfirst(‘respect’); | Outputs Respect |
lcfirst | Make the first character of a string value lower case | echo lcfirst(‘RESPECT’); | rESPECT |
strtolower() function- returns strings in lower case.
<?php $str = "My name is KHAN"; $str = strtolower($str); echo $str; ?>
Output:
my name is khan
strtoupper() function-returns strings in lower case.
<?php $str = "My name is KHAN"; $str = strtoupper($str); echo $str; ?>
Output:
MY NAME IS KHAN
ucfirst() function- returns 1st character of a string statement in upper case.
<?php $str = "my name is KHAN"; $str = ucfirst($str); echo $str; ?>
Output:
My name is KHAN
lcfirst() function- returns 1st character of a string statement in upper case.
<?php $str = "MY name IS KHAN"; $str = lcfirst($str); echo $str; ?>
Output:
mY name IS KHAN
ucwords() function- returns 1st letter of each word of a string statement in upper case.
<?php $str = "my name is Sonoo jaiswal"; $str = ucwords($str); echo $str; ?>
Output:
My Name Is Sonoo Jaiswal
strlen() – Return the Length of a String
<?php $str = "my name is Sonoo jaiswal"; $str = strlen($str); echo $str; ?>
Output:
12
str_word_count() – Count number of Words in a String
<?php echo str_word_count("Hello world!"); // outputs 2 ?>
Output:
2
strrev() – Reverse a String
<?php $str = "my name is Sonoo jaiswal"; $str = strrev($str); echo $str; ?>
Output:
!dlrow olleH
strpos() – Search For a Text Within a String
<?php echo strpos("Hello world!", "world"); // outputs 6 ?>
Output:
6
str_replace() – Replace Text Within a String
<?php echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly! ?>
Output:
Hello Dolly!
https://php.net/manual/en/ref.strings.php