What is it?

String is a Data type that is used to store charachters , yes plural not singular. in fact string is an array of chars that create a string.

Here are some examples:

$str=”This is a String.”;// just defined a variable that holds the string of: This is a String.

echo “This is another String!”;// this one will display the string and the message is: This is another String!

You can access acertinncharachterr in the string using array keys:

$str=”This is a Text”;

echo $str[0];// will display : T

You can find how long is the string using a PHP functionstrlenn():

$str=”This is a Text”;

echo strlen($str);//displays: 14  which means an array with keys from 0 to 13.

You can add strings together using “.” operator:

$str1=”This is”;

$str2=”a Text”;

echo $str1.$str2;// will show:This isa Text

echo $str1.” “.$str2;// will show: This is a Text

Strings are  a very handy data type as they can hold almost everything as string in them for example you can put numbers in them as string which means they will be treated by PHP as String and not numbers and then all you need to do is a function to convert it or just type cast it:

$str=”1234″; //defined 1234 as a string and not a number

$num=224;// defined as a number and not a string

$num2=(integer)$str;// type cast it so it converts to an integer

$str2=(String)$num; // this will convert the number into string

echo $num+$num2; // displays: 1458 which is 1234+224

You can also search a string for a specific peice of it using strpos() function for example:

$search _in_this_string=”This is a Text”;


echo strpos($search _in_this_string,$searching_for);// will display: 5

And you can do a lot more with Strings…

Here is the list of functions related to Strings in PHP, you don’t need to learn them all now just know that they are there that’s all you need to know for now. Here is the link: http://uk2.php.net/manual/en/book.strings.php

%d bloggers like this: