Jump to content
xisto Community
rkage

Tutorial: Variables In PHP Strings

Recommended Posts

Hopefully you have read my introduction to PHP and you are either thinking - "Wow, PHP seems really cool, I want to learn more!" or else "What's the point?" Both are valid thoughts. I am going to show you something that PHP is useful for and hopefully you will all be thinking the first thought.

 

You may be familiar with the x letter in algebra if you have ever done maths. Tell me what the value of x is? You can't, because it doesn't have one. You must give it a value. You may also be familiar with the value of pi - 3.14159..... (and so it continues on to infinity). pi has a value and if you put it on a page and send it to China, the guy there will know exactly what it is even if you haven't said.

 

So is x the variable or is pi the variable? it is x. Pi is a constant. x can have the value of 0,1, -34.4343434343 or infinity. It's value varies. OK so say you don't know alot about maths, let me explain it another way because the variables are the most important things about PHP in my opinion.

 

It's your birthday today. You get a present. What does the present contain? You don't know! A present can be a car or a pair of underwear. The present is a variable. No suppose you got a PLaystation 2 for your birthday...well then you got a playstation 2 which is a ....playstation 2. It can't be a giraffe, it is a constant so to speak.

 

So now that you know what a variable is, you must now know there are seven of them...just like the seven wonders of the world, the seven sins or the seven dwarves. Don't worry too much about what they mean just understand what the difference is.

 

String - The most simplest one. Contains characters - literally a string of characters. eg. "Playstation 2", "giraffe".

Integer - Contains a whole number that can be positive or negative. eg. "2", "-45", "0".

Float - Contains decimal numbers and really big numbers. eg. "45.5555", "560,304,506,454,454,000"

Boolean - Contains true or false that's all.

Array - A complex variable that may contain other variables.

Object - The most complex variable as it may contain other variables and it's own functions!

 

So in this tutorial, I'll try and cover strings. You will use these the most so they are very important.!

 

 

 

Strings!

 

1. Defining a variable!

 

$present = "giraffe";

All variables contain the "$" sign whether it be arrays, strings or integers. The $ must be followed by a letter. It is also best to work constantly in lower case letters, you'll understand why later. So we create the variable and give it a value of "giraffe" which is a string!

 

Strings can be put in double or single quotes (same rules apply for the echo function explained in the introduction when using the quotes and backslashes for escaping.) Remember the semi colon!

 

We can change a variable multiple times in our script because it can indeed vary...

$present = "playstation 2";//They ran out of PS2 so I had to but you a new present!$present = "Learn Algebra in 24 hours book";

2. Outputting the value of a variable

 

We output code by using the echo command again.

$present = "Playstation 2";echo $present;

Although, that on it's own is very useless since we define the variable and then echo the variable...we could just echo the present straight away. But suppose you have the present appearing many times in a paragraph...

$present = "Pair of Underwear";echo "We got you a $present for your birthday since you have always been looking for a $present!";

That will give a result of - "We got you a Pair of Underwear for your birthday since you have always been looking for a Pair of Underwear!" So yes we can infact put variables in the echo command along with other text thanks to PHP finding the special dollar sign character and treating the words afterwards as a variable.

 

So take a look at the following code and spot the error...

$present = "Playstation 2";echo "You may like your $present, it cost us $100 to buy!";

Well PHP well come across the present variable and process it as usual. Then it comes to the next dollar sign and thinks that the text after that is a variable. Hence you will get an "Undefined Error" warning. (You will also likely get another error since you can't put a number straight after the dollar sign in a variable.)

 

To fix this we use the magic backslash, which hopefully you are starting to see the wonder of.

$present = "Playstation 2";echo "You may like your $present, it cost us \$100 to buy!";

3. Changing part of a string.

 

In order to best explain this part, we will play a mini game. Yay! Due to the powers of PHP, we can change part of a string such as the first character or the 15th character to a different value. So for example;

$present = "Playstation 1";$present{12} = "2";echo $present;

Effectively, we have bought the latest Playstation as opposed to the older one. Points to note are that PHP treats every character in the string as a value including the spaces and that the first character is actually character "0".

 

So we can do this also

$present = "Playstation 2";$present{0} = "F";$present{1} = "i";$present{2} = "r";$present{3} = "e";echo $present;

So now we get "Firestation 2". But we can also use this to find the first letter of a string...

$present = "Giraffe";$first_letter = $present{0};echo "I'll give you a clue, your present begins with a $first_letter!";

There is also an important element in there and that is we can make one variable have the same value as another variable.

 

$ryans_present = "Football";$garys_present = $ryans_present";

In this case they both get the same present...a football!

 

So I did say we would do a game so here it is, using everything you've learnt. Tell me what value well be outputed. Then run the script and see if you were right!

 

$start = "game";$word1 = $start;$start{0} = "s";//$start{3} = "L";$start{2} = "i";$word0 = $start{2};$start{2} = "m";$word0{1} = $start{0};$end = $start;$word1 = "n";$word1{2} = "t";$start{0} = "g";$word2 = "cool";$word1{1} = "o";echo "This $start $word0 $word1 $word2";

There are many other functions that include strings but before we move on to those we need to learn about integers, booleans and functions to fully understand how we can manipulate text strings.

Share this post


Link to post
Share on other sites

2. Outputting the value of a variable

...

...

 

So take a look at the following code and spot the error...

$present = "Playstation 2";echo "You may like your $present, it cost us $100 to buy!";

Well PHP well come across the present variable and process it as usual. Then it comes to the next dollar sign and thinks that the text after that is a variable. Hence you will get an "Undefined Error" warning. (You will also likely get another error since you can't put a number straight after the dollar sign in a variable.)

 

To fix this we use the magic backslash, which hopefully you are starting to see the wonder of.

$present = "Playstation 2";echo "You may like your $present, it cost us \$100 to buy!";

<{POST_SNAPBACK}>


I'm using php5.0.4 and I got: âYou may like your Playstation 2, it cost us $100 to buy!â without fix it with the magic backslash. Please rkage, can you explain me why?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.