WikiGalaxy

Personalize

PHP Strings

Concatenation

Combining Strings:

In PHP, strings can be concatenated using the `.` operator. This allows you to combine multiple string values into a single string.


<?php
  $firstName = "John";
  $lastName = "Doe";
  $fullName = $firstName . " " . $lastName;
  echo $fullName; // Outputs: John Doe
?>
    

Point Heading:

Concatenation is useful for creating dynamic strings by combining variables and literal strings.

Console Output:

John Doe

String Length

Measuring Length:

The `strlen()` function in PHP is used to find the length of a string, which is the number of characters it contains.


<?php
  $string = "Hello, World!";
  $length = strlen($string);
  echo $length; // Outputs: 13
?>
    

Point Heading:

Knowing the length of a string is crucial for operations like validation or formatting output.

Console Output:

13

String Functions

String Manipulation:

PHP offers a variety of functions for string manipulation, such as `strtolower()`, `strtoupper()`, and `ucwords()`.


<?php
  $text = "hello world";
  echo strtoupper($text); // Outputs: HELLO WORLD
  echo strtolower($text); // Outputs: hello world
  echo ucwords($text); // Outputs: Hello World
?>
    

Point Heading:

These functions are ideal for formatting strings for display purposes.

Console Output:

HELLO WORLD
hello world
Hello World

String Searching

Finding Substrings:

The `strpos()` function is used to find the position of the first occurrence of a substring in a string.


<?php
  $text = "Find the position of a substring in this text.";
  $position = strpos($text, "substring");
  echo $position; // Outputs: 21
?>
    

Point Heading:

This function is particularly useful for parsing strings and extracting information.

Console Output:

21

String Replacement

Replacing Parts of Strings:

The `str_replace()` function replaces all occurrences of a search string with a replacement string.


<?php
  $text = "Replace the word 'world' with 'PHP'";
  $newText = str_replace("world", "PHP", $text);
  echo $newText; // Outputs: Replace the word 'PHP' with 'PHP'
?>
    

Point Heading:

This function is essential for data sanitization and dynamic content generation.

Console Output:

Replace the word 'PHP' with 'PHP'

String Splitting

Dividing Strings:

The `explode()` function splits a string into an array based on a delimiter.


<?php
  $text = "apple,banana,cherry";
  $fruits = explode(",", $text);
  print_r($fruits); // Outputs: Array ( [0] => apple [1] => banana [2] => cherry )
?>
    

Point Heading:

This function is useful for processing CSV data or parsing user input.

Console Output:

Array ( [0] => apple [1] => banana [2] => cherry )

String Joining

Combining Array Elements:

The `implode()` function joins array elements into a single string with a specified separator.


<?php
  $fruits = array("apple", "banana", "cherry");
  $text = implode(", ", $fruits);
  echo $text; // Outputs: apple, banana, cherry
?>
    

Point Heading:

This function is beneficial for creating readable lists from array data.

Console Output:

apple, banana, cherry

Multiline Strings

Handling Multiline Text:

PHP provides the `heredoc` and `nowdoc` syntax for handling multiline strings, offering a more readable format.


<?php
  $heredoc = <<<EOD
  This is a
  multiline string
  example.
EOD;
  echo $heredoc;
?>
    

Point Heading:

Using `heredoc` or `nowdoc` simplifies the process of embedding large blocks of text.

Console Output:

This is a
multiline string
example.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025