PHP String Examples



PHP Concatenate Two strings.
<?php
	$str1 = 'Hello John';
	$str2 = ' How are you? ';
    echo $str1.$str2;
?>
Example to repeat string.
<?php
	$str = 'Welcome';
	echo str_repeat($str, '10');
?>
Get length of a given string
<?php
	$st = 'file does not exist';
	echo strlen($st);
?>
Convert string in uppercase
<?php
	$st = 'Hello John, How are you?';
	echo strtoupper($st);
?>
Get length of a string
<?php
	$str = 'file does not exist';
	echo str_len($str);
?>
Replace word in a string
<?php
	$st = 'file does not exist';
	echo str_replace('document', 'file', $st);
?>
Get substring selection
<?php
	$str = 'Take what you need, and leave the rest behind';
	echo substr($str, 5, 13);
?>
Capitalize the first letter of a string
<?php
	$str = 'Take what you need, and leave the rest behind';
	echo ucfirst($str);
?>
Capitalize the first letter of each word in a string
<?php
	$str = 'Take what you need, and leave the rest behind';
	echo ucwords($str);
?>


Read more articles


General Knowledge



Learn Popular Language