PHP: Constants

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

The syntax for defining constant is as below:

bool define ( string $name , mixed $value [, bool $case_insensitive = false ])

<?php

define("hello", "Hi There.");
echo hello; 
echo "<br>";
echo Hello; 
echo "<br>";

//To allow case insensitive, set the third parameter to be true.
define("greeting", "How are you?", true);
echo GREETING; 
echo "<br>";
echo Greeting;

?>
Try The Code

Note that there are 2 parts in the above code.  The top part is case sensitive and the second part is case insensitive.

You can try to run the code here.

PHP: Associative array

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

Beside the indexed array, PHP also supports associative array.

Here, in the example below, we show how an associative array is formed.

<?php

$car = array('weight' => '1000kg', 'year' => 2004, 'price' => 7000);

echo "Weight of car: " . $car['weight'];
echo "\n";
echo "Year of car: " . $car['year'];
echo "\n";
echo "Price of car: " . $car['price'];
echo "\n";

?>

PHP: $_POST

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

$_POST is an associative array of variables passed to the current script via the HTTP POST method.

<?php
$_POST['name'] = "Michael";
echo 'Hello ' . htmlspecialchars($_POST['name']) . '!';
?>

You can try to run the script here.

The output in the browser will be:

Hello Michael!

$_POST is mainly used in form processing.  It is an associative array and so we can pass the name Michael using $_POST[‘name’].

PHP: $_GET

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

$_GET is an associative array of variables passed to the current script via the URL parameters.

<?php
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
?>

Try to click on this URL:  https://codecrawl.com/code/get.php?name=Daryl

You will get this output in the browser:

Hello Daryl!

Notice how I have passed the name Daryl into the script through the URL.

PHP: $_SERVER

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

$_SERVER — this is for obtaining server and execution environment information.

It is an array containing information such as headers, paths, and script locations.

The entries in this array are created by the web server.

<?php 
echo $_SERVER['SERVER_NAME'];
echo "\n";
echo $_SERVER['PHP_SELF'];
?>

You can try the script here.

It will output something like this:

www.codecrawl.com
/code/g_server.php

.