Question

I am trying to represent a character by its hex value inside a string. It works if hex is explicit but does not work when hex variable is used:

$hex = '5A';
echo "Hex explicit: \x5A, Hex evaluated: \x$hex";

The output is:

Hex explicit: Z, Hex evaluated: \x5A

The question is - how do I modify \x$hex to get Z instead of \x5A?

Was it helpful?

Solution

Hope this helps.

Integer literals

<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?> 

This is the answer

$hex = chr(0x5A);
echo "Hex explicit: \x5A, Hex evaluated:".$hex;

or

$hex = chr(0x5A);
echo "Hex explicit: \x5A, Hex evaluated: $hex";

Try this link http://www.w3schools.com/php/func_string_chr.asp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top