Site icon T4Tutorials.com

How to show ASCII Values in PHP?

ASCII Values program in PHP

<?php
$str = "t";
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
 $byte = substr($str, $pos);
 echo $str.' has value ' . ord($byte).PHP_EOL;
}
?>

Output

t has value 116

How to show ASCII Values in PHP with the form?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

<form  method="POST"> 

Enter Character: <input type="text" name="1"><br>


<input type="submit"value="Print ASCII" name="submit">

</form>

<?php
if (isset($_POST['submit'])){


$str = $_POST['1'];;
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
 $byte = substr($str, $pos);
 echo $str.' has value ' . ord($byte).PHP_EOL;
}}
?>            
            
</body>
</html>

Output

Figure: How to show ASCII Values in PHP

Topic Covered

How to show ASCII Values in PHP?

Exit mobile version