PHP Interview Questions - Strings 🎯

beginner
18 min

PHP Interview Questions - Strings 🎯

Welcome to our comprehensive guide on PHP strings! This tutorial is designed for both beginners and intermediates, aiming to provide a thorough understanding of strings in PHP. Let's dive in! 🐳

What is a String in PHP? πŸ“

A string is a sequence of characters in PHP, enclosed within single quotes (') or double quotes ("). Strings can include letters, digits, symbols, and spaces.

php
$myString = 'Hello, World!'; // Single quotes $anotherString = "Welcome to CodeYourCraft!"; // Double quotes

πŸ’‘ Pro Tip: Use single quotes for simple strings and double quotes for strings containing variables or complex expressions.

Basic String Operations 🎯

Length of a String

To find the length of a string, use the strlen() function.

php
$myString = 'CodeYourCraft'; $length = strlen($myString); echo $length; // Output: 12

Concatenation

To combine two or more strings, use the . operator (dot).

php
$firstName = 'John'; $lastName = 'Doe'; $fullName = $firstName . ' ' . $lastName; echo $fullName; // Output: John Doe

Accessing Characters

To access a character at a specific position (index) in a string, use the square bracket notation ([]).

php
$myString = 'CodeYourCraft'; $firstCharacter = $myString[0]; echo $firstCharacter; // Output: C

Substrings

To get a substring (a portion of a string), use the square bracket notation with the starting and ending positions. Remember, PHP uses zero-based indexing.

php
$myString = 'CodeYourCraft'; $substring = $myString[2]; // First character $substring = $myString[2, 5]; // Characters from the 2nd to the 6th echo $substring; // Output: deYour

String Functions πŸ“

PHP provides various built-in functions to work with strings. Here are a few examples:

Uppercase and Lowercase

To convert a string to uppercase, use the strtoupper() function. To convert a string to lowercase, use the strtolower() function.

php
$myString = 'CodeYourCraft'; $uppercaseString = strtoupper($myString); $lowercaseString = strtolower($myString); echo $uppercaseString, "\n"; // Output: CODEYOURCRAFT echo $lowercaseString, "\n"; // Output: codeyourcraft

Replace

To replace a substring within a string, use the str_replace() function.

php
$myString = 'CodeYourCraft'; $newString = str_replace('C', 'P', $myString); echo $newString; // Output: PodeYourCraft

Quiz 🎯

Question: What is the output of the following code?

php
$myString = 'PHP is awesome!'; $length = strlen($myString); echo $length;

A: 3 B: 12 C: 15 Correct: B Explanation: The output is 12, as strlen() counts all the characters in the string, including spaces.


This is the first part of our PHP strings tutorial. In the next section, we'll explore more advanced topics like string manipulation, formatting, and regular expressions. Stay tuned! 🎯


Note: This tutorial is just a starting point. For more in-depth learning, practice exercises, and real-world examples, visit CodeYourCraft regularly. Happy coding! πŸŽ‰