Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: Compiler Part 2 - December 3, 2018 :: Challenges 7 - December 7, 2018 :: Network App - December 14, 2018 >>

Database Programming :: Lessons :: PHP

Structure of Web Services

A web browser is used to format and display web pages. A web server sends web pages to the browser and responds to requests from visitors to the website. A database server is a type of information server and responds to queries for information from its database.

Web Server
© Cloudways Ltd.

HTTP is the protocol used to allow browsers and servers to communicate. A web server responds to requests for web pages and returns a new web page to the browser. Server-side languages such as PHP are programming languages that reside on the server to process requests. Server-side languages can be used to process more complicated requests and serve dynamic results.

PHP Basics

PHP was designed to help developers create dynamic and data-driven websites. It is a server-side language, therefore, you must upload your PHP pages to the server or install PHP on your local computer to use it. You can embed PHP in HTML documents as long as they have the .php extension. HTML can also be embedded in PHP scripts. Two great resources for learning about PHP are the following:

Like most languages, all statements in PHP end with a semicolon, unless that statement begins and ends with curly brackets { }. The echo function can be used to output text (and HTML) from a PHP script.

<?php
    echo("Hello world");
?>

The <?php and ?> tags are used to denote the begin and end of PHP code. They are required in every PHP script and whenever you embed PHP in an HTML document. If you create the above script and upload it to your web server, navigating to the page will simply display the text "Hello world." Viewing the source code will not reveal the underlying PHP code. Below is the same example, but embeding the PHP code in an HTML document:

<html>
    <head>
    	<title>PHP Embedded in HTML</title>
    </head>
    
    <body>
    	<p>
            <?php echo("Hello world"); ?>
        </p>
	</body>
</html>

PHP Variables

All variables in PHP are preceded by a dollar sign ($) and follow typical programming language naming conventions meaning variable names can contain letters, numbers, and a limited subset of symbols such as - and _. PHP is a loosely-typed language, which means variables do not have to be declared ahead of time.

If a variable is defined at the beginning of a PHP file it remains in memory until the end of the file. If a variable is assigned a value in a file that calles a second PHP file, and the second PHP file has a variable of the same name, the second variable will replace the value of the first variable.

Superglobals are predefined variables in PHP and available in all scripts. The following are some of the superglobals available in PHP:

To define a constant in PHP you can use PHP's define() function. You do not use a $ when accessing a constant.

<?php
    define("SCHOOL", "Yorkville High School");
    
    $mySchool = $_POST['school'];
    
    if ($mySchool == constant("SCHOOL"))
    	echo("Go Foxes!");
?>

Java vs PHP

Java and PHP have many similarities. The following are exactly the same in PHP and Java:

Equality in PHP can be done using ==, but == does not compare variable types. This can lead to some hard to diagnose errors. When comparing data in PHP that comes from outside the script it is best to use ===, which compares value as well as variable type.

Arrays in PHP

Arrays work similarly in PHP as they do in Java. You can define an array like the following:

<?php
    $pokemonStarters = array("Bulbasaur", "Charmander", "Pikachu", "Squirtle");
    
    echo($pokemonStarters[2]); //Outputs "Pikachu"
?>

An associative array stores data in key/value pairs like the following:

<?php
    $pokemonStarters = array("Bulbasaur"=>"Grass/Poison", "Charmander"=>"Fire", "Pikachu"=>"Electric", "Squirtle"=>"Water");
    
    echo($pokemonStarters["Bulbasaur"]); //Outputs "Grass/Poison"
?>

The count() function can be used to return the number of elements in an array. There are nearly 60 functions you can use with arrays. Arrays are a pivotal part of PHP since all of the data you receive from outside PHP scripts is sent via superglobals, which are stored as arrays.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram