Strings

Sequence of characters, usually assigned to a variable.

 

<?php
	$myLocation = "London" ;

	$myString = "I live in $myLocation" ; //will interpolate London in place of $myLocation

	$yourString = 'I live in $myLocation' ; //will literally use the text $myLocation
?>

Displays:

I live in London
I live in $myLocation

 

 

Concatenation

Joining of strings by use of the dot . operator.

 

<?php
	$stringOne = "London" ;

	$stringTwo = "I live in " ;

	echo $stringTwo . $stringOne ;
?>

Displays:

I live in London

 

 

A few String Functions

 

strlen($myVar) ; //returns the number of characters in the string

 

strpos("I live in London", "London") ; //crude example, strings could be replaced by variables

 

explode($myVar) ; //breaks a string into an array

 

implode(" ", $myArray) ; //returns a string from the elements of an array. Note use of optional separator

 

md5($myVar) ; //calculates the MD5 hash of a string

 

md5_file($fileHandle) ; //calculates the MD5 hash of a file

 

strtolower($myString) ; //converts string to lowercase letters

 

strtoupper($myString) ; //converts string to uppercase letters

 

Leave a Reply