Tag Archives: chr() function in php

what is chr() function in PHP ? How to get character value from ASCII value?

31 Dec

In PHP chr() function is used to get character from the given ASCII character.

Lets have an example for chr() function to get corresponding result :

<?php
$data1 = chr(65);
$data2 = chr(90);
$data3 = chr(64);

echo $data1."<br />";
echo $data2."<br />";
echo $data3."<br />";
?>

Output of above an example will be:

A
Z
@

Even using chr()  and rand() function we can generate captcha for human verification code.

Lets have an example for the same:

<?php
$data = "";
for($i = 1 ; $i<=5 ;$i++)
{
   $data.= chr( rand(65,90) );
}
echo " Captcha generated by random function and chr function is : ".$data
?>

From the given above example we understand that $data will have total 5 random character from ASCII value 65 to 90 ( i.e  from A to Z ) using rand() function.

rand() function in php will generate number from the given range. ( i.e. 65 to 90 in the given example ).

Now this random will get converted in character using chr() function of PHP.

Output of above example will be :

Captcha generated by random function and chr function is : ABDTP