So, what does that exactly mean?
- Code: Select all
$y = 1;
$x1 = "test";
echo $x$y;
In that example, the script would parse the variables name to be $x1. From there PHP would move forward to echoing $x1. So the script would print out the value test which we assigned to X1.
You will learn about arrays shortly, but here is a cool script you can play with.
I will be using the random function, which is quite simple.
- Code: Select all
random(1,5);
How does this work?
It picks a random between the first number in the second number.
In any PHP function, you can use a variable as input so we could try something like this:
- Code: Select all
$max = 5;
$min = 1;
random($min, $max);
At this stage our script we are not doing anything with the output of random so we will get an error.
Here is a fix:
- Code: Select all
$max = 5;
$min = 1;
$random = random($min, $max);
Now, lets fix this up and make a random quotation script!
- Code: Select all
$quote1 = "Hey, this is the first quote";
$quote2 = "Hey, the second quote could be first?";
$quote3 = "The second quote could be first, but I could be third and better than the second post";
$quote4 = "Quote 4 is cool!";
$quotecount = 4;
$randomquotenum = random(1, $quotecount);
$randomquote = $quote$randomquotenum;
echo $randomquote;
You can of course change any of the quotes, add new ones and remove quotes. Make sure to update $quotecount.
I want you to also know, that this is definately not the best (or easiest) way to print random quotes. You will always use the random function, but you must also learn arrays.
discuss this topic to forum
