Filed under: PHP
PHP $_COOKIE
This variable is often used to identify a user. This is stored on the user’s computer so that every user will be identified if who are they. You can create and get the value of a cookie.
setcookie() function
This function is used to create a cookie. This should appear before the <html> tag.
Syntax:
setcookie (name, value, expire, path, domain);
Example (create a cookie):
<?php
setcookie(“firstname”, “Bryan”, time()+7200);
?> //this should be written before the <html> tag
Here is an example on how to retrieve the value of a cookie:
<?php
echo $_COOKIE[“firstname”];
?>
isset() function
This can be used to check if a cookie has been set.
Example (using isset() function):
<html>
<body>
<?php
if (isset($_COOKIE[“firstname”]))
{
echo “You are “ . $_COOKIE[“firstname”];
}
else
{
echo “Please input your firstname”;
}
?>
</body>
</html>
You can view all cookies by this:
<?php
print_r($_COOKIE);
?>
You can delete a cookie by setting its expiration date in the past like this:
<?php
setcookie(“firstname”,””,time()-7200);
?>
Filed under: PHP
PHP $_REQUEST
This variable contains the content of both $_GET, $_COOKIE, and $_POST.
This can get the values passed from the form with both the GET and POST methods.
Here are 2 examples:
<form action=”myphp.php” method=”POST”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>
<?php
echo “Firstname: “ $_REQUEST[“firstname”]; . <br />;
echo “Lastname: “ $_REQUEST[“lastname”];
?>
OR
<form action=”myphp.php” method=”GET”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>
<?php
echo “Firstname: “ . $_REQUEST[“firstname”] . “<br />”;
echo “Lastname: “ . $_REQUEST[“lastname”] ;
?>
Filed under: PHP
PHP $_POST
This variable is used to get a value from a form. Unlike $_GET, $_POST is invisible to users and will not display the user’s input in the URL and has no limit.
Example:
<form action=”myphp.php” method=”POST”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>
When the user clicks the submit button, it will not display the user’s input in the URL like the $_GET. It will simply look like this:
http://myphp.com/myphp.php
Now, we can use the two values passed from the form by using the $_POST variable:
<?php
echo “Firstname: “ $_POST[“firstname”]; . <br />;
echo “Lastname: “ $_POST[“lastname”];
?>
Filed under: PHP
PHP $_GET
This variable is used to get a value from a form. Below is an example:
<form action=”myphp.php” method=”GET”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>
When the user clicks the submit button, it will then display a different URL depending on the user’s input. I’ll use Bryan for first name and King for last name so the URL will look like this:
Http://www.myphp.com/myphp.php?firstname=Bryan&lastname=King
Now, we can use the two values passed from the form by using the $_GET variable:
<?php
echo “Firstname: “ . $_GET[“firstname”] . “<br />”;
echo “Lastname: “ . $_GET[“lastname”] ;
?>
Filed under: PHP
PHP Functions
In this tutorial, i will teach you how to make functions in PHP and how to use it. A function is a block of code that we can call and use whenever we want.
Below is an example of a function that will display the text “learn PHP” when called:
<html>
<body>
<?php
function displayMessage()
{
echo “learn PHP.”;
}
displayMessage();
?>
</body>
</html>
Another example where we will be using the function we made:
<html>
<body>
<?php
function displayMessage()
{
echo “learn PHP.”;
}
echo “I want to “;
displayMessage();
?>
</body>
</html>
Output is:
I want to lean PHP
PHP Functions – Adding Parameters
We can also add parameter to our functions so it can have different outputs depending on the user input. A parameter is just like a variable. We should place the parameters inside the parentheses like this displayMessage($myMessage).
Here is an example where it will display our inputted message:
<html>
<body>
<?php
function displayMessage($myMessage)
{
echo $myMessage . “. <br />”;
}
echo “My message is “;
$displayMessage(“Welcome to my blog, if you have any questions just leave a comment”);
?>
</body>
</html>
Output is:
My message is Welcome to my blog, if you have any questions just leave a comment.
Here is another example where we will be having 2 parameters:
<html>
<body>
function displayMessage($myMessage, $punctuation)
{
echo $myMessage . $punctuation . “<br />”;
}
echo “My message is “;
displayMessage(“Please register here”,”.”);
?>
</body>
</html>
Output is:
My message is Please register here.
PHP Functions – Return Values
Functions can also return values.
Here is an example:
<html>
<body>
<?php
function fullName($fname, $lname)
{
$fullname = $fname . ” ” . $lname;
return $fullname;
}
echo”My full name is ” . fullName(“Bryan Joseph”,”King”);
?>
</body>
</html>
Output is:
My full name is Bryan Joseph King
Filed under: PHP
This is used to execute same codes at specified number of times.
There are four PHP looping statements:
- while – will continue looping through the code as long as the condition is true
- do…while – loops through the code then if the condition is true then it continues otherwise it will end the loop
- for – loops through the code while the condition is true
- foreach – loops through the code for each element in an array
The while statement
This will continue to execute the code as long as the condition is true. Below is the syntax:
while (condition)
code to be executed;
Below is an example:
It will continue to execute the code as long as the variable x is less than 3. Variable x will increase by 1 every time the code is executed.
<html>
<body>
<?php
$x = 0;
while($x<3)
{
echo “Display me 3 times. <br />”;
$x++;
}
?>
</body>
</html>
The do…while statement
Loops through the code then if the condition is true then it continues otherwise it will end the loop.
Below is the syntax:
do
{
code to be executed;
}
while (condition);
Below is an example:
It will continue to execute the code as long as the variable x is less than 3. Variable x will increase by 1 every time the code is executed.
<html>
<body>
<?php
$x=0;
while ($x<3)
{
echo “display me 3 times. <br />”;
$x++;
}
?>
</body>
</html>
The for statement
Loops through the code while the condition is true. Below is the syntax:
for (initialization; condition; increment)
{
code to be executed;
}
Below is an example:
It will display the text “Display me 3 times.” 3 times.
<html>
<body>
<?php
for($x=0;$x<3;$x++)
{
echo “Display me 3 times. <br />”;
}
?>
</body>
</html>
The foreach statement
This is used to loop through arrays. For every loop, the value of the current array is assigned to $value so on the next loop, you’ll be looking at the next element. Below is the syntax:
foreach (array as value)
{
code to be executed;
}
Below is an example:
<html>
<body>
<?php
$myarray = array(“Learn”, “PHP”,”Now”);
for each($myarray as $value)
{
echo $value . “<br />”;
}
?>
</body>
</html>
Filed under: PHP
PHP Strings
A String is used to store and manipulate a piece of text. This data type is used when we want to store character strings. In this tutorial, i am going to discuss some common functions and operators used to manipulate strings in PHP. Below is an example:
<?php
$txt = “Welcome.”;
echo $txt;
?>
Output:
Welcome.
The Concatenation Operator
There is only one string operator in PHP and this is the dot (.) operator and is used to combine to strings together. Below is an example:
<?php
$firsttext = “Learn”;
$secondtext = “PHP”;
echo $firsttext . ” ” . $secondtext;
?>
Output:
Learn PHP
The strlen() function
This is used to get the length of the string. Below is an example:
<?php
echo strlen(“Learn PHP”);
?>
Output:
9
Filed under: PHP
The Switch Statement
Use this if you have many codes to be executed and you want to execute only one.
Switch statement takes a single variable as input and then checks it against all different cases you set up for the switch statement then if a match was found then it executes the code otherwise if no match was found then the default case will be executed. Break is also important so that when a match is found, the statement will end right away and will not execute the remaining part of the statement.
Here is the syntax:
switch (variable)
{
case label1:
code to be executed if variable is equals to label1;
break;
case label2:
code to be executed if variable is equals to label2;
break;
default:
code to be executed if variable is not equals to label1 and label2;
}
Here is an example:
<html>
<body>
<?php
switch ($y)
{
case 1:
echo “y equals to 1″;
break;
case 2:
echo “y equals to 2″;
break;
case 3:
echo “y equals to 3″;
break;
default:
echo “y is not equals to 1, 2 or 3″;
}
?>
</body>
</html>
Filed under: PHP
The If…Else Statement
You can use this if you want to execute a code if the condition is true or a different code if the condition is false. Here is the syntax
if (condition)
the code you want to be executed if the condition is true
else
the code you want to be executed if the condition is false
Here is an example
This will output “You are Bryan.” if the value of $name is Bryan, otherwise it will output “You are not Bryan.”.
<html>
<body>
<?php
$name = “Bryan”;
if ($name == “Bryan”)
echo “You are Bryan.”;
else
echo “You are not Bryan.”;
?>
</body>
</html>
If the code you want to be executed contains more than one line then you should enclose it with curly braces like this:
<html>
<body>
<?php
$name = “Bryan”;
if ($name == “Bryan”)
{
echo “You are Bryan.”;
echo “<br /> Hi!”;
}
else
echo “You are not Bryan.”;
?>
</body>
</html>
The ElseIf Statement
If the code you want to be executed is more than 2 then use the ElseIf Statement. Below is the syntax
if (condition)
the code you want to be executed if the condition is true
elseif (condition)
the code you want to be executed if the condition is true
else
the code you want to be executed if the condition is false
Here is an example:
This will output “You are Bryan.” if the value of $name is “Bryan” and “You are Joseph.” if the value of $name is “Joseph”, otherwise it will output “You are not Bryan.”.
<html>
<body>
<?php
$name = “Bryan”;
if ($name == “Bryan”)
echo “You are Bryan.”;
elseif ($name == “Joseph”)
echo “You are Joseph.”;
else
echo “You are not Bryan.”;
?>
</body>
</html>