August 13, 2017

Core PHP: $_SERVER Variables: Host Name (7.1)

$_SERVER


$_SERVER['HTTP_HOST'] returns the Host header from the current request.
<?php
echo $_SERVER['HTTP_HOST'];
//Outputs "localhost"
?>

This method can be useful when you have a lot of images on your server and need to transfer the website to another host. Instead of changing the path for each image, you can do the following:
Create a config.php file, that holds the path to your images:
<?php
$host = $_SERVER['HTTP_HOST'];
$image_path = $host.'/images/';
?>

Use the config.php file in your scripts:
<?php
require 'config.php';
echo '<img src="'.$image_path.'header.png" />';
?>

The path to your images is now dynamic. It will change automatically, based on the Host header.

This graphic shows the main elements of $_SERVER.
Figure: the main elements of $_SERVER
Figure: the main elements of $_SERVER

Core PHP: $_SERVER Variables: Script Name (7.0)

Predefined Variables


A superglobal is a predefined variable that is always accessible, regardless of scope. You can access the PHP superglobals through any function, class, or file.

PHP's superglobal variables are $_SERVER, $GLOBALS, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION.

$_SERVER

$_SERVER is an array that includes information such as headers, paths, and script locations. The entries in this array are created by the web server.
$_SERVER['SCRIPT_NAME'] returns the path of the current script:
<?php
echo $_SERVER['SCRIPT_NAME'];
//Outputs "/somefile.php"
?>

Our example was written in a file called somefile.php, which is located in the root of the web server.

Core PHP: The Return Statement (6.4)

Return


A function can return a value using the return statement.
Return stops the function's execution, and sends the value back to the calling code.
For example:
function mult($num1, $num2) {
  $res = $num1 * $num2;
  return $res;
}

echo mult(8, 3);
// Outputs 24

Leaving out the return results in a NULL value being returned.
A function cannot return multiple values, but returning an array will produce similar results.

Core PHP: Function Parameters (6.3)

Default Arguments


Default arguments can be defined for the function arguments.
In the example below, we're calling the function setCounter(). There are no arguments, so it will take on the default values that have been defined.
function setCounter($num=10) {
   echo "Counter is ".$num;
}
setCounter(42);  //Counter is 42
setCounter();  //Counter is 10

When using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.

Core PHP: Function Parameters (6.2)

Function Parameters


Information can be passed to functions through arguments, which are like variables.
Arguments are specified after the function name, and within the parentheses.
Here, our function takes a number, multiplies it by two, and prints the result:
function multiplyByTwo($number) {
  $answer = $number * 2;
  echo $answer;
}
multiplyByTwo(3);
//Outputs 6

You can add as many arguments as you want, as long as they are separated with commas.
function multiply($num1, $num2) {
  echo $num1 * $num2;
}
multiply(3, 6);
//Outputs 18

When you define a function, the variables that represent the values that will be passed to it for processing are called parameters. However, when you use a function, the value you pass to it is called an argument.

Core PHP: User-Defined Functions (6.1)

Functions


In the example below, we create the function sayHello(). The opening curly brace ({) indicates that this is the beginning of the function code, while the closing curly brace (}) indicates that this is the end.
To call the function, just write its name:
function sayHello() {
  echo "Hello!";
}

sayHello(); //call the function

//Outputs "Hello!"

Core PHP: User-Defined Functions (6.0)

Functions:

A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads. It will be executed by a call to the function.
A user defined function declaration starts with the word function:
function functionName() {  
   //code to be executed
}


A function name can start with a letter or an underscore, but not with a number or a special symbol.
Function names are NOT case-sensitive.