Accessing a Specific Character in String in PHP

Q

How To Access a Specific Character in a String?

✍: FYIcenter.com

A

Any character in a string can be accessed by a special string element expression:

  • $string{index} - The index is the position of the character counted from left and starting from 0.

Here is a PHP script example:

<?php 
$string = 'It\'s Friday!';
echo "The first character is $string{0}\n"; 
echo "The first character is {$string{0}}\n"; 
?>

This script will print:

The first character is It's Friday!{0}
The first character is I

 

Assigning a New Character in a String in PHP

Including Array Elements in Double-Quoted Strings in PHP

Understanding PHP String Literals and Operations

⇑⇑ PHP Tutorials

2016-10-13, 3153🔥, 0💬