Docsity

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

Toggle sidebar
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!":

strrev() - Reverse a String

The PHP strrev() function reverses a string.

Example

Reverse the string "Hello world!":

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!":

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":

ucfirst()

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

Example

Output:

Php programming

lcfirst()

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

Example

Output:

pHP Programming

ucwords()

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

Example

Output:

Php Programming