Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

PHP String Functions

The document provides an overview of PHP string functions and methods for creating strings, including single quotes, double quotes, heredoc, and nowdoc. It explains how to manipulate strings using various functions such as strtolower, strtoupper, and substr, among others. Additionally, it highlights the importance of understanding string handling in PHP for effective development.

Uploaded by

mirkhales
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PHP String Functions

The document provides an overview of PHP string functions and methods for creating strings, including single quotes, double quotes, heredoc, and nowdoc. It explains how to manipulate strings using various functions such as strtolower, strtoupper, and substr, among others. Additionally, it highlights the importance of understanding string handling in PHP for effective development.

Uploaded by

mirkhales
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

KAZI A S M-9765645688

PHP String Functions: substr, strlen, strtolower, explode, strpos, str_replace


What is a string?
A string is a collection of characters. String is one of the data types supported by PHP.
The string variables can contain alphanumeric characters. Strings are created when;
 You declare variable and assign string characters to it
 You can directly use them with echo statement.
 String are language construct, it helps capture words.
 Learning how strings work in PHP and how to manipulate them will make you a very
effective and productive developer.
PHP Create strings
Let’s now look at the four different ways of creating strings.
Creating 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
var_dump('You need to be logged in to view this page');
?>
Output:
string(42) "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.
The code below illustrates how to escape a single quote.
<?php
echo 'I \'ll be back after 20 minutes';
?>
Output:
I'll be back after 20 minutes
PHP Create Strings Using Double quotes
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
$name='Alicia';
echo "$name is friends with kalinda";
?>

HERE,

1
KAZI A S M-9765645688

 The above example creates a simple string with the value of Alicia.
 The variable name is then used in the string created using double quotes and its value is
interpolated at run time.
Output:
Alicia is friends with kalinda
In addition to variable interpolations, the double quote string can also escape more special
characters such as “\n for a linefeed, \$ dollar for the dollar sign” etc.
More examples Let’s suppose that we have the following code
<?php $pwd = "pas$word"; echo $pwd; ?>
Output:
NOTICE : Undefined variable
pas
executing the above codes issues a notice “Notice: Undefined variable”.
This is because $word is treated as a variable.
If we want the dollar sign to be treated as a literal value, we have to escape it.
<?php
$word="word";
$pwd = "pas\$word";
echo $pwd; ?>
Output:
pas$word
PHP Heredoc
This heredoc methodology is used to create fairly complex strings as compared to double quotes.
The heredoc supports all the features of double quotes and allows creating string values with
more than one line without php string concatenation.
Using double quotes to create strings that have multiple lines generates an error.
You can also use double quotes inside without escaping them.
The example below illustrates how the Heredoc method is used to create string values.
<?php

$baby_name = "Shalon";

echo <<<EOT

When $baby_name was a baby,

She used to look like a "boy".

EOT;

?>
HERE,
<<<EOT is the string delimiter.
EOT is the acronym for end of text.

2
KAZI A S M-9765645688

It should be defined in its on line at the beginning of the string and at the end.
Note: you can use anything you like in place of EOT

Output:
When Shalon was a baby, She used to look like a "boy".
PHP Nowdoc
The Nowdoc string creation method is similar to the heredoc method but works like the way
single quotes work.
No parsing takes place inside the Nowdoc.
Nowdoc is ideal when working with raw data that do not need to be parsed.
The code below shows the Nowdoc implementation
<?php

$baby_name = "Shalon";

$my_variable = <<<'EOT'

When $baby_name was a baby,

She used to look like a "boy".

EOT;

echo $my_variable;

?>
Output:
When $baby_name was a baby, She used to look like a "boy".

3
KAZI A S M-9765645688

PHP string functions


PHP string functions are used to manipulate string values.
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 echo strtolower( 'Benjamin'); outputs benjamin
lower case letters
strtoupper Used to convert all string characters to echo strtoupper('george w outputs GEORGE W
upper case letters bush'); BUSH
strlen The string length function is used to echo strlen('united states of 24
count the number of character in a string. america');
Spaces in between characters are also
counted
explode Used to convert strings into an array $settings = explode(';', Array ( [0] =>
variable "host=localhost; db=sales; host=localhost [1] =>
uid=root; pwd=demo"); db=sales [2] => uid=root
print_r($settings); [3] => pwd=demo )
substr Used to return part of the string. It $my_var = 'This is a really This is a re...
accepts three (3) basic parameters. The long sentence that I wish to
first one is the string to be shortened, the cut short';echo
second parameter is the position of the substr($my_var,0, 12).'...';
starting point, and the third parameter is
the number of characters to be returned.
str_replace Used to locate and replace specified echo str_replace ('the', 'that', that laptop is very
string values in a given string. The 'the laptop is very expensive
function accepts three arguments. The expensive');
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.
strpos Used to locate the and return the position echo strpos('PHP 4
of a character(s) within a string. This Programing','Pro');
function accepts two arguments
sha1 Used to calculate the SHA-1 hash of a echo sha1('password'); 5baa61e4c 9b93f3f0
string value 682250b6cf8331b
7ee68fd8
md5 Used to calculate the md5 hash of a string echo md5('password'); 9f961034ee 4de758
value baf4de09ceeb1a75
str_word_count Used to count the number of words in a echo str_word_count ('This 12
string. is a really long sentence that
I wish to cut short');
ucfirst Make the first character of a string value echo ucfirst('respect'); Outputs Respect

4
KAZI A S M-9765645688

Function Description Example Output


upper case
lcfirst Make the first character of a string value echo lcfirst('RESPECT'); Outputs rESPECT
lower case
Summary
 A string is a set of characters
 single quotes are used to specify simple strings
 double quotes are used to create fairly complex strings
 heredoc is used to create complex strings
 Nowdoc is used to create strings that cannot be parsed.

You might also like