Well here are some cool variable effects.
Adding Multiple Strings
We can use strings to create great effects.
Example:
CODE
<?php
$var1="Hello person.";
$var2=str_replace("person","world",$var1);
echo $var1." ".$var2;
?>
$var1="Hello person.";
$var2=str_replace("person","world",$var1);
echo $var1." ".$var2;
?>
We used str_replace to show two sentences with strings.
Neat huh.
Random Integers
This is one of the sweetest features of PHP. the rand() function. Here is a neat trick using this function.
Example
CODE
<?php
function exVar()
{
$num=rand(1,100);
if ($num<100)
{
echo "The number is less than 100.";
}
else
{
echo "The number is 100.";
}
}
exVar()
?>
function exVar()
{
$num=rand(1,100);
if ($num<100)
{
echo "The number is less than 100.";
}
else
{
echo "The number is 100.";
}
}
exVar()
?>
That will run the function and tell you if it is 100 or less. Great for guessing games.
MySQL Connect
With variables we make connecting to a MySQL database clean and simple
Most mysql connection codes are like this.
CODE
<?php
$localhost="localhost";
$name="username";
$password="password";
$database="db";
$connect=mysql_connect($localhost,$name,$password) or die(mysql_error());
mysql_db_select($database,$connect);
?>
$localhost="localhost";
$name="username";
$password="password";
$database="db";
$connect=mysql_connect($localhost,$name,$password) or die(mysql_error());
mysql_db_select($database,$connect);
?>
Simple huh! Variables make it look simpler and neater.
Superglobals
This is another awesome feature of PHP and variables. The $_POST and $_GET variables. We can use these to receive user input. We also can use the $_GET global to host a small website in ONE FILE.
$_POST is very widely used in registration scripts and chatboxes.
Example of $_GET
CODE
<?php
if ($_GET['var'] == 'var')
{
//Post some html/php stuff here
}
else
{
//post the main page code here
}
?>
if ($_GET['var'] == 'var')
{
//Post some html/php stuff here
}
else
{
//post the main page code here
}
?>
Example of $_POST
CODE
<?php
$name=$_POST['var'];
echo $name;
?>
<form action="page.php" method="post">
<input type="text" name="var" />
<input type="submit" value="Submit" />
</form>
$name=$_POST['var'];
echo $name;
?>
<form action="page.php" method="post">
<input type="text" name="var" />
<input type="submit" value="Submit" />
</form>
The first code gives us a one file site using the $_GET variable and If statements.
Our second code processes the form and prints the user input.
Thank you for reading my tutorial.


