PHP: Using $GLOBALS instead of global

This entry is part 11 of 54 in the series PHP Tutorial

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

The previous example can be rewritten as:

<?php
$a = 1;
$b = 2;

function Sum()
{
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
} 

Sum();
echo $b;
?>

The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element.

Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

PHP: Variable with global keyword

This entry is part 10 of 54 in the series PHP Tutorial

In PHP, global variables must be declared global inside a function if they are going to be used in that function.

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version.

PHP: Variable scope

This entry is part 7 of 54 in the series PHP Tutorial

The scope of a variable is the context within which it is defined.

Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?>

If you type the code into a good IDE such as phpStorm, you will receive a complain from the editor saying that $a is an undefined variable.

The above script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope.

You may notice that this is different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition.

PHP: A simple form using $_POST

This entry is part 37 of 54 in the series PHP Tutorial

After you have installed the WAMP server into your PC, we are ready to work on a simple form.

I have tried  a few online php code editors and they are not suitable for creating forms and so you can try the code below on your own PC.

<html>
<body>

<form action="" method="post">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit">
</form>

</body>
</html>
<html>
<body>

<?php
$your_name = $_POST["name"];
$your_email = $_POST["email"];
echo "Welcome " . $your_name."<br>";
echo "Your Email is " . $your_email; ?>

</body>
</html>

You can try the form here.

Notice how the name and email are passed into the PHP code using

$_POST[“name”]
$_POST[“email”]

The $_POST is a superglobal variable.  They are global variables defined in PHP and they are accessible anywhere in the code.

As we have put the php code in the same form as the HTML form, we might see warnings related to the 2 lines below.

$your_name = $_POST["name"];
$your_email = $_POST["email"];

We can put @ in $_POST as in

$your_name = @$_POST["name"];
$your_email = @$_POST["email"];

This is to prevent the warning message as we have put the name variable in the input form into the php code.  You can also use it without the @ but a warning message could be rendered in the page.

Usually we would have separated the HTML form with the php code by putting the php script in a separate file as shown in the next example.

 

PHP: Apache Web & PHP server

This entry is part 36 of 54 in the series PHP Tutorial

Now we are going to do some serious programming stuff.

Most of the online PHP codepads or fiddles do not support form and HTML very well.  PHP is known for its ability to work well with the backend database.

We will have to create some HTML forms and collect some data and send them to the MySQL database. I suggest that you download a copy of WAMP server and install it in your PC.

WampServer is a Windows web development environment. It allows you to create web applications with Apache2, PHP and a MySQL database.

Alongside, PhpMyAdmin allows you to manage easily your databases.

First download the WAMP server and install it to C:\wamp.  From the startup menu in your PC, start WAMP server.

Once the WAMP server is started, the Apache web server will be started.

Type localhost in your browser and you will see a page similar to below:

wamp