Strings are vital in PHP, and are probably one of the most used parts of this language. String variables contain characters; non-numerics. Before a string can be used, it has to be created. So let's go ahead and declare a string variable.
$string = "This is a sentence; in programming it's called a string";
That will display:
This is a sentence; in programming it's called a string
What we did was declare a variable, $string, and now it holds This is a sentence; in programming it's called a string as it's information.
Think of it like this; the variable is a cup and the information is the coffee. A cup can be empty, but it's more useful if it's full.
In a previous class, we learned about echo'ing. If you haven't figured out why line #4 would not work, it's because we used three single-quotes instead of just two. When we echo a string, you have to be a little careful about the quotes. Here's what I mean:
$string = 'This is a string, isn't it great?!';
/* the single-quote between the n and the t in isn't makes PHP think the string has ended, but it's not followed by a semicolin - so it will error. */
This can become a problem. How do we fix it? It's called escaping. We escape special characters that you want to show up as a regular character, so it doesn't mess things up.
Here and example:
$string = 'This is a string, isn\'t it great?!';
/* notice the extra forward slash infront of that apostrophe? That tells PHP it's a regular apostrophe, and to not stop the string from ending */
There is, however, a major difference between double quotes and single doubles. Double quotes allow for special escaped characters to be used, but single-quotes dont.
$doubleNewline = "1. creating a new line, you use \n"; // \n creates a new line - not in HTML though.
$doubleReturn = "2. a carriage return is \r"; // \r create a carriage return, like hitting enter when typing up a document. Not for HTML either, use <br /> or <p></p> for those.
$doubleTab = "3. tabs are \t neat!"; // \t will create a tab
$doubleEscape = "4. escaping a double quotes like so: \", will work as well"; // same escaping rule applies
$doubleDollar = "5. the \$ sign needs to be escaped as well. If we don't, it tries to call upon a variable";
$single = '6. \t \r \n " $';
This will display as:
1. creating a new line, you use
2. a carriage return is
3. tabs are neat!
4. escaping double quotes like so: ", will work as well
5. the $ sign needs to be escaped as well. If we don't, it tries to call upon a variable
6. \t \r \n " $
Strings can be joined together, with variables or with other parts of a string through PHP's concentation, the period.
$age = 21;
$str = "My age is " . $age . " but I look 30";
We decalred two variables. $age and $str.
$age has no quotes, because it's an integer.
$str starts as a string, but instead of using a semicolin after the word is, we used a period and typed the $age variable. If we wanted to end it here, we could have use a semicolin after $age. But we wanted more! So we added another period, and created another string looking part, then added the semicolin.
Oh yes, we can manipulate almost everything in today's society, including sentences. Without getting too far in depth, we'll go through a couple functions that can be very useful in the future.
strlen(), ucwords(), strtoupper(), and strtolower() are all usefull functions
For these next three examples, assume that
$str = "hello world";
Let's go through these...
String Length. Counts the characters in each string. Accepts text and variables.
echo strlen($str); // displays 11
echo strlen("Hello World"); // displays 11
Uppercase words. Turns all words that start with a lowercase letter, into a capital. Accepts text and variables.
echo ucwords($str); // displays Hello World
echo ucwords("hello world"); // displays Hello World
Makes all letters uppercase. Accepts text and variables.
echo strtoupper($str); // displays HELLO WORLD
echo strtoupper("hello world"); // displays HELLO WORLD
Makes all characters lowercase. Accepts text and variables.
echo strtolower($str); // displays hello world
echo strtolower("HeLlO wOrLd"); // displays hello world
For a large list of other functions to do with strings, reffer to the PHP.net String Functions Manual
Create a new .php called strings.php.
Declare a 2 variables: an integer, and a string.
echo them in the same line, followed by a line break (html <br /> is ok to use)
Then echo the string as uppercase.
Then echo the string len (strlen()) at the end.
It doesn't need to be pretty, do the best you can.
Any questions or problems, feel free to message me or post a question.