Strings in PHP

Getting Started with PHP
Getting Started with PHP : Home
Introduction
Installation
Hello World
Data Types in PHP
Variables in PHP
Operators in PHP
Decision Making in PHP
Iterations in PHP
Functions in PHP
Arrays in PHP
Strings in PHP
Object Oriented PHP
Classes and Objects
Constructors
Inheritance
Object Interfaces
Static and Final
Advanced Features in PHP
GET and POST
Session in PHP
Cookies in PHP
Server Variables

String
String is an array of characters. You can create a string simpy with the use of "" or '' and the content between quotes. Here are some strings.

<?php
$str = "This is my first string";
echo $str."<br>";
$str_num = "You can have number as well 1232";
$string = "13212";
echo $str_num."<br>";
echo $string;
?>


Output
This is my first string
You can have number as well 1232
13212

Concatenating Strings
We have used . operator so many times while printing. This operator is to concatenate two strings. You can simply add two string using . operator.
String Functions
There are lots of function for manipulating strings and some of those are :,

<?php
$str = "This is my first string";
// Length of string
echo strlen($str)."<br>";
// replacing my with your
echo str_replace("my","your",$str)."<br>";
// trimming Th 
echo trim($str,"Th")."<br>";
// Converting to lower case
echo strtolower($str)."<br>";
// Metaphonee equivalent
echo metaphone($str);

// Note that none of these make changes to the original variable
// They return manipulated string which you can use or store
?>


Output
23
This is your first string
is is my first string
this is my first string
0SSMFRSTSTRNK

Login to Track Your Progress