PHP ord() Function with Example

By IncludeHelp Last updated : December 27, 2023

PHP ord() function

The ord() function is a string function, it is used to get the ASCII code of the first character of the given string.

Syntax

The syntax of the ord() function:

ord(string);

Parameters

The parameters of the ord() function:

  • string: An input string parameter.

Return Value

The return value of this method is string, it returns the number value (ASCII code).

Sample Input/Output

Input: "Abc"
Output: 65

Example of PHP ord() Function

<?php
$str = "Abc";
echo ("ASCII code of first character is = " . ord($str) . "\n");

$str = "Hello world";
echo ("ASCII code of first character is = " . ord($str) . "\n");	

$str = "_okay";
echo ("ASCII code of first character is = " . ord($str) . "\n");	

$str = "123Hello";
echo ("ASCII code of first character is = " . ord($str) . "\n");	
?>

Output

The output of the above example is:

ASCII code of first character is = 65 
ASCII code of first character is = 72 
ASCII code of first character is = 95 
ASCII code of first character is = 49 

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.