Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PHP Strings.................................., Study notes of Programming Languages

PHP Strings.........................................................

Typology: Study notes

2019/2020

Uploaded on 11/04/2020

sundar-c
sundar-c 🇮🇳

1 document

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP Strings
A string is a sequence of characters, like "Hello world!".
PHP String Functions
strlen() - Return the Length of a String
The PHP strlen() function returns the length of a string.
Example
Return the length of the string "Hello world!":
<?php
echo strlen("Hello world!"); // outputs 12
?>
str_word_count() - Count Words in a String
The PHP str_word_count() function counts the number of words in a string.
Example
Count the number of word in the string "Hello world!":
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
strrev() - Reverse a String
The PHP strrev() function reverses a string.
Example
Reverse the string "Hello world!":
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
pf3
pf4
pf5

Partial preview of the text

Download PHP Strings.................................. and more Study notes Programming Languages in PDF only on Docsity!

PHP Strings

A string is a sequence of characters, like "Hello world!".

PHP String Functions

strlen() - Return the Length of a String

The PHP strlen() function returns the length of a string.

Example

Return the length of the string "Hello world!":

str_word_count() - Count Words in a String

The PHP str_word_count() function counts the number of words in a string.

Example

Count the number of word in the string "Hello world!":

echo str_word_count("Hello world!"); // outputs 2

?>

strrev() - Reverse a String

The PHP strrev() function reverses a string.

Example

Reverse the string "Hello world!":

echo strrev("Hello world!"); // outputs !dlrow olleH

?>

strpos() - Search For a Text Within a String

The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

Example

Search for the text "world" in the string "Hello world!":

echo strpos("Hello world!", "world");// outputs 6

?>

str_replace() - Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.

Example

Replace the text "world" with "Sachin":

echo str_replace("world", "Sachin", "Hello world!"); // outputs Hello Sachin!

?>

ucfirst()

The ucfirst() function returns string converting first character into uppercase. It doesn't change the case of other characters.

Example

$str="php programming";

$str=ucfirst($str);

echo $str;

?>

Output:

Php programming

lcfirst()

The lcfirst() function returns string converting first character into lowercase. It doesn't change the case of other characters.

Example

$str="PHP Programming";

$str=lcfirst($str);

echo $str;

?>

Output:

pHP Programming

ucwords()

The ucwords() function returns string converting first character of each word into uppercase.

Example

$str="php programming";

$str=ucwords($str);

echo $str;

?>

Output:

Php Programming