You are here

Week 4 - Understanding and Creating PHP scripts

After developing queries in postgres database - metabase from datanearhere and dataexplore, I had to understand the next step using PHP scripts.

Definition: PHP is a script language and interpreter that is freely available and used primarily on Linux Web servers. PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym."

To understand PHP language is different from other programming script language that I understand, therefore it is new to me to develop PHP script for the datanearhere files. Making it hard to create the script, so my mentor Charles had me create a PHP script to understand how it works. creating a basic PHP script called "Hello World!" save as helloworld.php.

The default file extension for PHP files is ".php". A PHP file normally contains HTML tags, and some PHP scripting code. Below, we have  a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!":

PHP statements end with a semicolon (;).


echo "Hello World!";


Creating  PHP Variables

PHP Variables

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for PHP variables:

  1. A variable starts with the $ sign, followed by the name of the variable

  2. A variable name must start with a letter or the underscore character

  3. A variable name cannot start with a number

  4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

Output Variables

The PHP echo statement is often used to output data to the screen. The following example will output the sum of two variables:

$Wage = 14.75;
$MonHours = 7.5;
$TuesHours = 6.5;
$WedsHours = 7.25;
$ThursHours = 8.5;
$FriHours = 7.25;

$WeeklyHours = $MonHours + $TuesHours + $WedsHours + $ThursHours + $FriHours;
$WeeklyEarnings = $Wage * $WeeklyHours;



output: 547.225

PHP is a Loosely Typed Language. In the script above, notice that we did not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value. In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.

Next I am working how to connect to server to the cmop database, where I create php script with the queries I created in the metabase with datanearhere and dataexplore files.