Image may be NSFW.
Clik here to view.Now, after we learned how to show content and also had a first glance at variables, we will now go a step further.
To define a variable in PHP you just need to use $ and a variable name: $variable
.
You can use as variable name whatever you like, however you are not allowed to let it start with a number, like $1variable
and there are also some predefined variables, about which I will talk later.
There are many types of variables, but for now we concentrate on 4 types (actually 5, but the fifth will follow in another tutorial).
So, which 4 types do we have?
string
e.g. $string = "This is a string";
boolean
e.g. $boolean = True;
integer
e.g. $integer = 123456;
float
e.g. $float = 1.234;
so, let’s make a script where can play a bit with those variables:
<?PHP
$string= "This is a string"; // String
$string_alt= 123456; // String
$boolean= True; // Boolean
$integer= 123456; // Integer
$float= 1.234; // Float
print 'The variable "$string" has the type of: '.gettype($string).'<br />';
print 'The variable "$string_alt" has the type of: '.gettype($string_alt).'<br />';
print 'The variable "$boolean" has the type of: '.gettype($boolean).'<br />';
print 'The variable "$integer" has the type of: '.gettype($integer).'<br />';
print 'The variable "$float" has the type of: '.gettype($float);
?>
Run this script and the gettype() function will tell you which type a variable has.
If you have a look at the output, you will see a mistake. Isn’t „$string_alt“ supposed to have the type „string“?
Yes, it is supposed to be a string, however, PHP does not know that your number shall be a string. So how can fix this problem?
It is quite easy, just add (string)
infront of it, so it looks like $string_alt= (string) 123456; // String
.
Everything should be fine now, you could even turn the string back to integer, just add (integer) again:
<?PHP
$string_alt= (string) 123456; // String
print 'The variable "$string_alt" has the type of: '.gettype($string_alt).'<br />';
$string_alt = (integer )$string_alt; // convert to Integer
print 'The variable "$string_alt" has the type of: '.gettype($string_alt);
?>
What we did here is called „type casting“ and it is always useful if you have script where you need to change the variable type to prepare it for a special function or whatever you plan to do with it. You can find a complete list of the type casting in the PHP documentation at the official website.
Type casting is not the only thing you can do variables. You can also add $variable1 at the end (or beginning) of $variable2:
<?PHP
$start = "The little brown fox ";
$end = "jumped over the green fence.";
$start_merge = $start;
$start_merge .= $end; // adds behind the existing content
print 'Start sentence: '.$start.'<br />';
print 'End sentence: '.$end.'<br /><br />';
print 'Correct sentence: '.$start_merge.'<br />';
print 'Correct sentence: '.$start.$end.'<br />';
?>
As you will see our sentence will be shown in different ways. So let us have a closer look at the code itself:
The „.=“ means, that the variable which shall be added to another one will be added at the end of the other variable
The „$start.$end“ looks, familiar, doesn’t it? I mentioned it in PHP Tutorial 101: Hello World! and the same behavior you see there also happens here.
Before I go on with some mathematical things we can do, I will tell you about the „//“ I added to the code because I assume you are wondering what it means Image may be NSFW.
Clik here to view.
The double-slashes only exclude a comment of the code. Beside the „//“ you can also use „/* comment */“ and „#“
The „//“ comment will end either at the end of the line of code where it is placed; Everything on the same line and behind of the slashes belong to the comment.
The „/* comment */“ starts at „/*“ and ends at „*/“ no matter if there are multiple lines of code or not.
The „#“ comment behaves like the „//“ comment.
So, as we have this explained, I’m able to use more comments in the code and avoid longer „outside of code“ explanations Image may be NSFW.
Clik here to view.
Now let’s do some mathematics!
With PHP (as with any other programming language) you are able to make all kind mathematic calculations. For this tutorial, I only stick to the most basic mathematic functions and refer you to the PHP Manual: Mathematical Functions
We will use the following script as a start for some calculations:
<?PHP
$a = 5;
$b = 2.3;
$c = -6;
$d = 100;
$ad = $a + $d; // Adds $d to $a
$da = $a - $d; // Subtracts $d from $a
$bc = $b / $c; // Divides $b through $c
$dc = $d * $c; // Multiplies $b with $c
print $a.' + '.$d.' = '.$ad.'<br />';
print $a.' - '.$d.' = '.$da.'<br />';
print $b.' / '.$c.' = '.$bc.'<br />';
print $d.' x '.$c.' = '.$dc.'<br />';
?>
We just did the basic mathematic calculations you and I think it is self-explanatory, so let’s have a look how we can get a random number, divide it by another random number and make sure the result is rounded to 2 decimal places, if it is a floating number:
<?PHP
$random_a = rand(1,1000); // get a random number between 1 and 1000
$random_b = rand(500,1500); // get a random number between 500 and 1500
$random = $random_a / $random_b;
$random_round = round($random,2); // rounds result to 2 decimal places
print 'First random number is: '.$random_a.'<br />
Second random number is: '.$random_b.'<br />
Result is: '.$random.'<br />
Rounded result is: '.$random_round;
?>
Of course there are way more things you can do, but I think this is enough for mathematics. I recommend you have a look at the PHP Manual: Mathematical Functions and play around because it is proved that you learn much more by the „Trial and Error“ concept that any other Tutorial.
A tutorial is always a start, but you really learn something by doing it on your own. So use the time to the next tutorial by training what you have learned. Image may be NSFW.
Clik here to view.
Update: Here you see a page with the results of the code used in this tutorial