PDA

View Full Version : Information PHP Tutorial - Variables



Master Jake
Jun 14th 2009, 12:42 AM
Welcome to the second php tutorial. Today we will be going over variables.

First thing you need to know is what variables are. Variables are basically temporary storage containers for data. A variable can contain a string, such as "Hello World", an integer such as 5, a float such as 3.98, etc.

Common Datatypes:
String - A series of characters. - "Hello World"
Integer - A numerical value (non-decimal) - 5
Double - A decimal value with more than 6 decimal places - 20.5848287389
Float - A decimal value with no mare than 6 decimal places - 12.11
Character - A single character - 'A'

There are more, but those are the common datatypes. You will need to know those for later.

Now, how exactly do you create a variable in php? Well, it's fairly simple. First of all, you need to use the dollar sign ($), followed by what you want to call the variable (the variable name), followed by an equals sign to show that we are giving it a value, followed by the value of the variable.

Here is a couple examples.



<?php

$myString = "This is a string";

$myInt = 20;

$myChar = 'B';

?>


Spaces are not required around the equals sign but do help readability. It is helpful to give the variable a name that will relate to the variable's value and help you remember what it is used for.

Now once you set a variable, how do you use it?

Well, there are multiple things you can do with variables, such as echo them out on the screen, do math with them, etc.

First, let's go over displaying them. To display a variable, you will need the "echo" command we learned from the last tutorial, and the variable name.

Here is an example.



<?php

$myVar = 20;

echo $myVar;

?>


PHP will then find the instance of "$myVar" and replace it with the value of "20". Notice how we don't use quotes around the variable when echoing it.

This is because, you only use quotes around strings, and variables are not strings, they are variables. If we did place quotes around "$myVar" it would have echo'd out the actual string "$myVar" instead of the value of 20 like we wanted.

Simple enough, right?

Let's continue on with math. Math is very easy to do in php. You just type out an equation and let php perform it. For example, 5 + 5 = 10, right? Well, you can assign that to a variable, or even assign a variable a value and use math on it in comparison to another variable.

For example:



<?php

$valOne = 20;
$valTwo = $valOne + 5;

echo $valTwo;

?>


When we ran this code, it would echo out "25" onto the screen. This is because we are assigning the value of "$valOne (20) + the value of 5 (25)" to the $valTwo variable.

There is plenty of other math you could do, that was just an example.

Finally, we will discuss linking text with variables with text and so on.

So, how would you link some variables and text together to display as one solid string on the page. Well, the string linker in php is the period (.). Here is an example of how this is used.



<?php

$myVal = 20;

echo "The value of $myVal is: " . $myVal;

?>


When this code is saved and ran it will display this text onto the screen:

The value of $myVal is: 20

This is because, we linked the string with the variable using the linker (.). You could also continue on and link the variable with other variables and strings to create one giant string.

For example.



<?php

$myVar = 5;
echo "Did you know that $myVar is equal to " . $myVar . "?";

?>


When executed, this code would generate this text onto the screen:

Did you know that $myVar is equal to 5?

Nifty, huh?

Keep in mind, again, that the spaces around the period is not needed, but it helps readability.

For example, you could write the above code as this:



<?php

$myVar = 5;
echo "Did you know that $myVar is equal to ".$myVar."?";

?>


Notice how it is all cluttered though?
Exactly.

Thanks for viewing the second php tutorial. I hope to see you in the next.

Copyright (c) 2009 Jake Chappell. All rights reserved.
skateparkceasercash@yahoo.com

FPG
Aug 23rd 2009, 06:45 PM
It should be noted in relation to strings that single and double quotes have different meanings.

When you're wanting to create a string with one of your variables you would usually do so like this:


$myNewString = "This is my new string, $myString";

The double quotes signifies in php to use the variable $myString in place of the text "$myString". No need for concatenation here.

The value of the variable $myNewString would be:


This is my new string, This is a string

However, if one were to use single quotes for the $myString variable like so:


$myNewString = 'This is my new string, $myString';

The value would be very different:


This is my new string, $myString

So when using single quotes it is necessary to concatenate your variables into your strings. For double quotes, you can just place your variables within the string. I just wanted to point this out because using double quotes can be a time saver (writing less code, especially if you're using lots of variables in your strings). However, the double edge sword of that is that php spends more time processing double quoted strings than single quoted strings (although that time is probably marginal). So if you're just using characters in your strings (no variables), use single quotes. The performance difference for each string is marginal, but it is good practice and in high traffic sites with lots of strings it could add up to something.

So in your tutorial you had this code:


<?php

$myVal = 20;

echo "The value of $myVal is: " . $myVal;

?>

The actual value of that echo would be: The value of 20 is: 20 because the double quotes signify to use the value of the variable not the text itself. Hope that helps!