Tag Archives: php tutorial for beginners with examples

PHP Implode function


PHP Implode function – It converts array into string. This function accepts array elements and returns the string of elements separated by delimiter. The function explode is reverse of the implode function. Both implode and explode functions are frequently used functions in php. We are going to describe the implode function with example and demo.


PHP Implode function Syntax

PHP Implode function Syntax

implode(separator, array);

Input Parameters

  • separator : Is used to put separator between array element you can pass null as “” if do not want to put anything between elements.
  • array : Array you want to convert to string.

Return value

It returns string.

Technical Requirement-

PHP Version : 4+

Let us understand implode function with examples.

PHP Implode function Example

Here is very basic example –

PHP implode example

$array = array('a','b','c','d');
$string = implode(" ", $array);
echo $string;

Try it »

In the above example array elements converted into string by separator space. The above example will produce the following outut-

PHP Implode function Example

PHP implode array comma separated Example

Here is another example with separator comma “,” –

PHP implode array comma separated

$array = array('a','b','c','d');
$string = implode(",", $array);
echo $string;

Try it »

The above example will produce following output-

PHP Implode function Example

PHP implode array pipe separated Example

Here is another example with separator pipepline “|” –

PHP implode array separator pipeline “|” Example

$array = array('a','b','c','d');
$string = implode("|", $array);
echo $string;

Try it »

The example will convert the array elements to string by pipeline separator. Here is the output of the above example –

PHP implode array comma separated string

If you want to perform reverse of this function use the function explode.

Note : PHP implode function is binary safe.

PHP htmlspecialchars Function


PHP htmlspecialchars Function : It converts Special Characters to HTML entities. This function returns the converted result as string. This function has its own significance and used frequently. The opposite of this functions is htmlspecialchars_decode(). We are going to explain this example with example and demo.

Let us understand the htmlspecialchars with very basic example – We have some raw data like this – <b>Hi it’s me John</b> now what would happen when user see form data on browser it will show something bold string like this – Hi it’s me John which is wrong because it should show output like this – <b>Hi it’s me John</b> . Now to fix this problem convert HTML tags to its equivalent entities. This is done by using the function htmlspecialchars().


PHP htmlspecialchars Function Syntax

It Converts the following HTML Characters only –

  • &(ampersand) = &
  • ” (double quote) = "
  • ‘ (single quote) = '
  • < (less than) = <
  • > (greater than) = >

Here is syntax for the function htmlspecialchars

Syntax

htmlspecialchars(string,flags,char-set,double_encode)

Input Parameters

  • string: String to be converted.
  • flag:flag is optional . This is basically used to specify how to handle the quotes and which document type use.

Here are following flags available-

  • ENT_COMPAT : This is default. Encodes only Double Quotes
  • ENT_QUOTES : This Encodes Both Single and Double Quotes.
  • ENT_NOQUOTES : Encodes neither Single nor Double Quotes.
  • ENT_IGNORE : Ignore invalid code unit sequences instead of returning empty string.
  • ENT_SUBSTITUTE : Replace invalid code unit sequence with a unicode replacement
    charecter U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string
  • ENT_DISALLOWED : Replace invalid code points in the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD;.
  • ENT_HTML401 : Handle code as HTML 4.01.
  • ENT_XML1 : Handle code as XML 1.
  • ENT_XHTML : Handle code as XHTML.
  • ENT_HTML5 : Handle code as HTML 5.
  • char-set: This is optional parameter. This is basically used to decide which character set to use.
    • UTF-8 : This is Default ASCII Compatible Multi byte 8-bit Unicode.
    • ISO-8859-1 : Western European, Latin-1.
    • ISO-8859-15 : Western European, Latin-9
    • cp866: DOS-specific Cyrillic charset
    • cp1251 : Windows-specific Cyrillic charset.
    • cp1252 : Windows specific charset for Western European
    • KOI8-R : Russian.
    • BIG5 : Traditional Chinese, Specially used in Taiwan.
    • GB2312 : National standard character set.
    • BIG5-HKSCS : Traditional Chinese.
    • Shift_JIS SJIS, SJIS-win, cp932, 932 Japanese
    • EUC-JP :Japanese
    • MacRoman : Charset that was used by Mac OS.
  • double_encode: This is optional parameter which decides whether to encode existing htmlentitis or not. It accepts TRUE and FALSE as parameter.
    • TRUE: Encode Everything.
    • FALSE: Do not encode existing htmlentities.

Return Values

This function returns the encodeed string.

Php Version

Supports in php 4+

PHP htmlspecialchars function example

PHP htmlspecialchars function example with single quotes

$string = "Hi it's me John";
echo htmlspecialchars($string);

Try it »

Above Example will produce following output-

HTML Output

PHP htmlspecialchars function example

Browser Output

PHP htmlspecialchars function example

PHP htmlspecialchars function example

PHP htmlspecialchars function example with double quotes

$string = '"Hello World"';
echo htmlspecialchars($string);

Try it »

HTML Output

PHP htmlspecialchars function example

Browser Output

PHP htmlspecialchars example

Note : Using htmlspecialchars is good practice. Use this when working with data output or working with characters and entities.

More About htmlspecialchars Funtion

Let’s have some more information about this function.


PHP htmlspecialchars vs htmlentities

The difference between htmlspecialchars and entities is given below-

  • htmlentities — Converts/Encodes all applicable characters to HTML entities.
  • htmlspecialchars — Converts/Encodes only special characters to HTML entities.
  • htmlspecialchars does not converts all characters having their html equivalent.
  • If output is XML you can’t use HTML entities in a XML file.
  • htmlentities converts more characters than htmlspecialshars so htmlspecialchars is more efficient.
  • If your page have encoding ASCII or LATIN-1 use htmlentities else use htmlspecialchars.

Read full documentations about htmlentities with example and demo – htmlentities

Tip : Use both the functions where they are needed because both have different efficiency.

PHP htmlspecialchars reverse

If want to perform reverse operation of htmlspecialchars use – htmlspecialchars_decode.

Try Video Demo – All In One Video for this method –

PHP htmlspcialchars function Video Demo –

PHP htmlspecialchars_decode Function


PHP htmlspecialchars_decode Function : It converts Some HTML entities to its characters equivalent. This function is opposite of htmlspecialchars() function. We are going to explain this example with example and demo.


PHP htmlspecialchars_decode Function Syntax

It Converts the following entities only –

  • & = &(ampersand)
  • " = ” (double quote)
  • ' = ‘ (single quote)
  • < = < (less than)
  • > = > (greater than)

Here is syntax for the function htmlspecialchars_decode

Syntax

htmlspecialchars_decode(string,flags)

Input Parameters

  • string: String to be converted.
  • flag:flag is optional . This is basically used to specify how to handle the quotes and which document type use.

Here are following flags available-

  • ENT_COMPAT : This is default. Converts only Double Quotes
  • ENT_QUOTES : This Converts Both Single and Double Quotes.
  • ENT_NOQUOTES : Converts neither Single nor Double Quotes.
  • ENT_HTML401 : Handle code as HTML 4.01.
  • ENT_XML1 : Handle code as XML 1.
  • ENT_XHTML : Handle code as XHTML.
  • ENT_HTML5 : Handle code as HTML 5.

Return Values

This function returns the converted string.

PHP htmlspecialchars_decode Function Example-

Let’s understand the above function with example and demo –

PHP htmlspecialchars_decode

$string = "

Hello World -> "

"; echo htmlspecialchars_decode($string);

Try it »

The above example will produce the following output-

Html Output

PHP htmlspecialchars_decode function

Browser Output

PHP htmlspecialchars_decode function


More Example

Let’s have a look over more example and demo here. Here we will explain example with flags.

PHP htmlspecialchars_decode

$string = "

Hello ' World ->Learn & More Here "

"; echo htmlspecialchars_decode($string, ENT_COMPAT)."
"; echo htmlspecialchars_decode($string, ENT_QUOTES)."
"; echo htmlspecialchars_decode($string, ENT_NOQUOTES)."
";

Try it »

The above example will produce the following output-

Html Output

PHP htmlspecialchars_decode example

Browser Output

Browser Output

PHP htmlentities function


PHP htmlentities function : This function coverts all applicable characters to the HTML entities. It accepts string as input parameter and returns the encoded string.

Let us understand the htmlentities with very basic example – Suppose you have a form and it has a text field textarea and user posts data using this field and you want print each and everything user submits using the form textarea. Suppose he enters raw data like this – <b>Hi its me John</b> now what would happen when user see form data on browser it will show something bold string like this – Hi its me John which is wrong because it should show output like this – <b>Hi its me John</b>.Now to fix this problem convet HTML tags to its equivalent entities. This is done by using the function htmlentities().

for example if you use this –

$string = "Hi its me John";
echo htmlentities($string);

Equivalent html entities will be –

PHP htmlentities function

This will print the output by converting them in entities like – <b>Hi its me John</b<

.

Note : This function is identical to function htmlspecialchars() almost in all ways except the characters which have HTML entity character entity equivalents are converted to entities.

We are going to explain this function with example and demo.


PHP htmlentities function Syntax

Here is syntax for htmlentities-

:

htmlentities(string,flags,char-set,double_encode);

Input Parameters

Desription about input parameters of htmlentities-

  • string: This is required input parameter which needs to be encoded.
  • flags:This is optional input parameter which handles single and double quotes. Here are following quote style-
    • ENT_COMPAT : This is default. Converts/Encodes only Double Quotes
    • ENT_QUOTES : This Decodes Both Single and Double Quotes.
    • ENT_NOQUOTES : Encodes neither Single nor Double Quotes.
  • char-set: This is optional parameter. This is basically used to decide which character set to use.
    • UTF-8 : This is Default ASCII Compatible Multi byte 8-bit Unicode.
    • ISO-8859-1 : Western European, Latin-1.
    • ISO-8859-15 : Western European, Latin-9
    • cp866: DOS-specific Cyrillic charset
    • cp1251 : Windows-specific Cyrillic charset.
    • cp1252 : Windows specific charset for Western European
    • KOI8-R : Russian.
    • BIG5 : Traditional Chinese, Specially used in Taiwan.
    • GB2312 : National standard character set.
    • BIG5-HKSCS : Traditional Chinese.
    • Shift_JIS SJIS, SJIS-win, cp932, 932 Japanese
    • EUC-JP :Japanese
    • MacRoman : Charset that was used by Mac OS.
  • double_encode: This is optional parameter which decides whether to encode existing htmlentitis or not. It accepts TRUE and FALSE as parameter.
    • TRUE: Convert Everything.
    • FALSE: Do not convert existing htmlentities.

Return Parameter

Returns the encoded string on the basis of the flags,char-set and double_encode parameters which are optional but plays and important role while encoding process. Here are some example which will make the things more clear about the PHP htmlentities function.

PHP htmlentities function

Here is very basic example of htmlentities function-

html entities function Example:

$string = ". Learn 'More' here now";
echo htmlentities($string);

Try It »

If you run the above example it produce the following output as below-

PHP htmlentities function

Note : The above image is HTML output. For Browser output click the below try button.

Try It »

The browser output will be something like this-

Html entities function example


More Examples

Let’s have look over more example and demo here.


htmlentities Example with ENT_COMPAT,ENT_QUOTES & ENT_NOQUOTES

htmlentities Example:

$string = "'Tutorialsplane Learn Easy!' &  Learn 'More' here now";
echo htmlentities($string,ENT_COMPAT)."
"; echo htmlentities($string,ENT_QUOTES )."
"; echo htmlentities($string,ENT_NOQUOTES)."
";

Try It »

The HTML Output of the above example is –

Html entities in php

The Browser Output of the above example is –

Php Htmlentities function browser output

If You want reverse of this function just use –

Learn More – html_entity_decode function


Secure Your Application Using htmlentities

Let us take a very important example which every developer should know and use while working with input fields and storing them in database and displaying them on front end.


Consider you have used htmlentities while displaying data in front end and someone inserts a few lines of javascript to redirect to some other location such as –

Unsafe Data in Html:


What would happen if this code is not encoded ?? it will consider that you have inserted above piece of javascript in html which will always redirect to the given url instead of printing the above code.

To fix the above problem use htmlentites which convet HTML tags to entities and will print instead on redirecting.

If you use html entities it will convert the above code as –

html entites

So now your html data is secure which will now print the below data instead of redirecting to another url-


PHP html_entity_decode function


PHP html_entity_decode function : It converts the html entities to its applicable characters. This function decodes all html entities to its equivalent characters. We will explain this function with Syntax, Example And Demo .


Note : It Supports In PHP 4 and above versions.

PHP html_entity_decode function

Here is syntax for html_entity_decode function

Syntax: html_entity_decode function

html_entity_decode(string,flags,char-set);

Input(html_entity_decode) Parameters

Hers are parameters explained –

  • string: This is required input parameter which needs to be decoded.
  • flags: This is optional input parameter which handles single and double quotes. Here are following quote style-
    • ENT_COMPAT : This is default. Converts/Decodes only Double Quotes
    • ENT_QUOTES : This Decodes Both Single and Double Quotes.
    • ENT_NOQUOTES : Decodes neither Single nor Double Quotes.
  • char-set: This is optional parameter. This is basically used to decide which character set to use.
    • UTF-8 : This is Default ASCII Compatible Multi byte 8-bit Unicode.
    • ISO-8859-1 : Western European, Latin-1.
    • ISO-8859-15 : Western European, Latin-9
    • cp866: DOS-specific Cyrillic charset
    • cp1251 : Windows-specific Cyrillic charset.
    • cp1252 : Windows specific charset for Western European
    • KOI8-R : Russian.
    • BIG5 : Traditional Chinese, Specially used in Taiwan.
    • GB2312 : National standard character set.
    • BIG5-HKSCS : Traditional Chinese.
    • Shift_JIS SJIS, SJIS-win, cp932, 932 Japanese
    • EUC-JP :Japanese
    • MacRoman : Charset that was used by Mac OS.

Return(html_entity_decode) Parameters

Returns the decoded string.

PHP html_entity_decode function

Here is very basic example of html_entity_decode function –

html entity decode function Example:

PHP html_entity_decode function

Try It>>

If you run the above example it produce the following example as below-

PHP html_entity_decode function

PHP explode function


PHP explode function : It breaks the string into an array. It takes string as an input and returns the array.


Here is the PHP Syntax

explode(separator, string, limit)

separator : Separator the delimiter in string from where to break it in array such as break a string where whitespace found.
string : String As Input.
limit : Is optional which specifies how many elements to return.

There are following possible limits :
a. limit > 0 : Return an array with max possible limit.
b. limit = 0 : Return an array with one element all string parts will be in one element of array.
c. limit < 0 : Return the array except the last element of array.

PHP explode Function Example 1 : Limit = 2 (>0)

$str = "a b c d e";
$array = explode(" " , $str, 2);
print_r($array);

Try it »

Output Will Be :

PHP explode function

PHP explode() function

PHP explode Function Example 2 : Limit = 0

$str = "a b c d e";
$array = explode(" " , $str, 0);
print_r($array);

Try it »

Output of the above example will be :

PHP explode function

PHP explode() function

PHP explode Function Example 3 : Limit = -1 (<0)

$str = "a b c d e";
$array = explode(" " , $str, -1);
print_r($array);

Try it »

PHP explode function

PHP explode() function

PHP echo function


PHP echo function: Echo function is used to print the strings in php.


PHP echo function Syntax

echo strings

strings : Input Strings.
Return : Will print string.

Note 1 : Parenthesis are not required as “echo” is not function actually. It is construct. So you need not to use parenthesis with the echo.

Note 2 : Echo is faster than the print so we prefer to use echo instead of print.

PHP echo Function Example 1

$string = "Hi It's me Kelly!";
echo $string;

Try it »

As you see in the above example we have not used the parenthesis like this :echo($string) which is incorrect.

The below example shows the concatenation of the two strings.

PHP echo Function Example 2

$string1 = "Hi I am John !";

$string2 = "A Young Developer.";

echo $string1.' '.$string2;

Try it »

Output of the above example will be :

PHP echo Function Example

PHP echo() Function Example

Let us print an array using echo.

PHP echo Function Example 3

$array = array("0"=>"Tani","1"=>"The PHP Developer");
echo $array[0].' '.$array[1];

Try it »

PHP crypt Function


PHP crypt Function : It encrypts the string using the DES, Blowfish, or MD5 algorithms.


PHP crypt Function Syntax

crypt(str,salt)

String : Input String
Salt : An additional string to make more secure encryption.
Return : Hashed String.

PHP crypt Function Example

$passwoord = "mypassword";
$str = crypt($passwoord);
echo $str;

Try it »

Output of the above example will be :

PHP crypt Function

PHP crypt() Function

PHP crc32 Function


PHP crc32 Function : It creates the 32-Bit Cyclic redundancy checksum of a string.


PHP crc32 Function Syntax

crc(string)

String : Input String
Output : 32-Bit CRC Result.

PHP crc32 Function Example

$string = "Hello World !";
$str = crc32($string);
echo $str;

Try it »

Output of the above example will be :

PHP crc32 Function Example

PHP crc32 Function Example