MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

PHP Super Global Arrays and Control Statements

This tutorial is about super global arrays and control statements like if, if-else, else-if and switch case.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

The super global arrays in PHP will help you find cookies information, how to send secure data to server and upload files, and create sessions which are variables that store information of user for later use.

SUPER GLOBAL ARRAYS IN PHP

Super global arrays are predefined arrays and can be accessed anywhere in the page, without using the word ‘global’. We will use the concept of arrays later, but here we will only describe super global arrays.

ARRAYS

DESCRIPTION

$GLOBALS

Contain all global scope variables.

$_SERVER

It contains all the data which the server sent in HTTP RESPONSE e.g page name which is currently running, server name, HTTP version number, and remote IP address.

$_GET

Contains all query string variables which are sent with the URL.

$_POST

Used to store data of ‘FORM FIELDS’ which we want to get on a targeted page.

$_COOKIE

Contains cookies data which the server send to client and is stored on client machine in form of a file, with the help of which we can identify a user.

$_FILES

Contains information of the file which we upload with help of POST METHOD.

$_SESSION

All the session variables are stored in this array.

$_REQUEST

By default this array contains the contents of $_GET, $_POST, and $_COOKIE.

Table 1: shows arrays and their description

USING SUPER GLOBAL ARRAYS POST AND GET

$_post

When we make a form on a page we use the method post and when we post it, all the form data goes into this array. With help of this array we can get all the data of the form easily on another page. To understand it better let’s use a form tag.

<form method =”post” action =”bool.php”>

We used two attributes in this form tag. One is the method and the other is action. The method tells us about that whether we are sending form data through “POST or GET method”. The second attribute action tells us about the page to which we are sending data e.g in this case “bool.php”. We normally give complete path in it, but here as both pages are in the same folder we didn’t have to.

So start your xampp, open your DreamweaveràFileàNewàPHPàCreate and save it ‘htdocs’ of xampp with a name of form.php.

Listing 1: posting a form through post method.

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>form</title>
  </head>
  <body>
  <form method="post" action="bool.php">
  please type your name:
  <input type="text" name="txtname" />
  <input type="submit" value="say hi"  />
  </form>
  </body>
  </html>
  

Figure 1: showing output of Listing 1 write your name in box.

When you click the button “say hi” it will show the error

Figure 2: shows that page bool.php does not exist.

Now making page bool.php

Listing 2: code for bool.php page to receive data form form.php

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>form data</title>
  </head>
   
  <body>
  <?php
  // we will get form data from form.php page  using $_POST array
  $name=$_POST['txtname'];
  print"hello "  .$name;
  ?>
  </body>
  </html>
  

Figure 3: showing output of Listing 2.

WORKING OF CODE IN LISTING 1 AND LISTING 2:

In form.php we made a form and in action attribute we use ‘bool.php’ which means that we are sending data to this page with the help of the post method. In this form we made a text box and gave it the name of ‘txtname’ in name attribute. You can use any name for form field but remember you will use that particular name for receiving data of that field. We also created a submit button with name ‘say hi’ which will submit form data to ‘bool.php’.

In bool.php we created a variable $name and assign it the value taken by post array from ‘txtname’.

The print will display a hello message to the name given in the box.

$­_GET

It is a super global array and contains query string information which are sent with the URL. For example

http://mrbool.com/articles/listcomp.asp?txtsearch=post+and+get&x=25&y=30

As shown in this example a query string is sent with the url with a name of ‘txtsearch’ and its value is ‘post+and+get&x=25&y=30’ . A query string in a URL starts with sign of interrogation (?).

So to get value of query string we use $_GET.

Listing 3: Code for sending query string data in URL through get method (get.php).

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>using get method</title>
  </head>
   
  <body>
  <a href="bool.php?site=mr.bool">sending querystirng information</a>
   
  </body>
  </html>
  

Figure 4: output of Listing 3 click on the link.

Listing 4:bool.php for receiving data through get array.

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>form data</title>
  </head>
   
  <body>
  <?php
   
  $sitename=$_GET['site'];
  print"Welcome to Mr.  "  .$sitename;
  ?>
  </body>
  </html>
  

Figure 5: showing query string in URL and also on page.

Control structures

If

The features of if statement in php are similar to that of C. If statement executes a section of code when a condition becomes true. For example

Listing 5: code to show syntax and use of if statement

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>use of if statement</title>
  </head>
   
  <body>
  <?php
  $x=5;
  $Y=7;
  if ($x<$Y){
                  print"x is less than y <br>";}
                  if ($x>$Y){
                                 print"x is greater than y <br>";}
                                 if ($x==$Y){print "x is equal to y <br>";
                                 }
                                 if ($x<=$Y){print" x is less than or equal to y <br>";}
                                 if ($x>=$Y){print"x is greater than or equal to y <br>";}
                                 if ($x!=$Y){print"x is not equal to y <br>";}
   
  ?>
  </body>
  </html>
  

Figure 6: showing output of listing 5.

USE OF IF ELSE STATEMENT

When a condition becomes false and we want to run another section of code we use else with if.

Syntax

If(expression){
condition true this code runs
}
else {
this executes}

for example

Listing 6: code for if-else

  <html>
  <head>
   
  <title>if-else</title>
  </head>
   
  <body>
  <?php
  $password=1234;
  if ($password==1234){
                  print"successful login";}
                  else{
                                 print"you entered wrong password";}
  ?>
  </body>
  </html>
  

Figure 7: shows output of listing 6.

In Listing 6 we simply assigned a number to a variable $password and checkedt whether it is correct or not.

Use of else if statement

When a condition becomes false in if statement and we want to run another condition for it we use else if.

Syntax
if(expression){
  //if true this code runs
  else if (expression){
  //if above condition is false and this is true, this code is executed and below blocks of if will not run}
  else{ //this code may execute in all other cases}
  

Listing 7: code for else if statement

  <html>
  <head>
   
  <title>else if</title>
  </head>
   
  <body>
  <?php
  $x=2;
  $y=8;
  if($x>$y){print"x is greater than y <br>";}
  else if($x<$y){print"y is greater than x <br>";}
  else{print"both are unequal";}
  ?>
  </body>
  </html>
  

Figure 8: output of Listing 7.

Switch statement

The switch statement is similar to a series of if statements but the difference is that for only one expression different blocks of codes are run.

Syntax

switch(expression){
case output1:
//if expression is equal to output1 this code may run
break;
case output2:
//if expression is equal to output 2this code may run
break;
default:
//no match this may run

}

Listing 8: code which will show use of switch.

   
  <html>
  <head>
   
  <title>switch</title>
  </head>
   
  <body>
   
  <?php
  if(isset($_POST['posted'])){
                  $usernumber=$_POST['digit'];
                  switch($usernumber){
                                 case 1:
                                 echo "you enter one";
                                 break;
                                 case 2:
                                 echo "you enter two";
                                 break;
                                 case 3:
                                 echo "you enter three";
                                 break;
                                 case 4:
                                 echo "you enter four";
                                 break;
                                 case 5:
                                 echo "you enter fivr";
                                 break;
                                 default:
                                 echo"invalid input";}}
  ?>
  <form method="POST" action="switch.php">
  <input type="hidden"  value="false" name="posted"><br>
  please enter a number between 1 and 5 <br>
  <input type="text" name="digit"><br>
  <input type="submit" value="submit"></form>
   
  </body>
  </html>

Figure 9: showing output of Listing 8 before and after entering a number in box.

Entering numbers in output of listening 8 which are not in range (1-5 ), the output will show you “invalid input” as shown in Figure 10.

Figure 10: showing output of Listing 8for a wrong number.

In this tutorial we learned about two super global arrays i.e. get and post. We will learn the use of other super global arrays later as we proceed further.

Another important topic discussed in this tutorial is use of conditional statements, which will help you in performing different actions based on different conditions in your code



Software developer doing B.E in Computer Science at Hamdard university karachi. Have good skills in php, html, javascript and css.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login