PHP — Basics

Summary

An simple exercise in learning the basic, common commands of PHP. I carried out this experiment initially for the portfolio section, but will expand onto the entire site, wherever appropriately applicable.

Example

Stage 1. Hello World

Stage 3. This is a variable-created string.
4x5=20

Stage 4. The following statement is done by calling a function and returning the result of the function
6 x 7 = 42

Stage 5. The following uses the "include()" function to include another php file.
Untitled Document

Hey everyone this sentence is written in html in external.php

hey everyone this sentence is writen in php in external.php.

Date Created

17 Sept 2008.

Help

  1. w3schools.com

Process

Really simple, follow-the-instructions from w3schools.com to practice writing it myself instead of copy and paste.

  1. Just write "Hello World."
  2. Write comments in PHP. You can't read it when it renders on the page.
  3. Make numerical variables and do math.
  4. Use a function to do the math and "return" the result.
  5. Use server-side include to insert an external file as if it's part of the current file.

The Code

Here’s the code. Please link back here if you’re using it.

Place this PHP in the body tag.

echo "Stage 1. Hello World"."<p />";

//You can't read this on the page, not even when you look at the source.

/* You can't read this either
*/

$text="Stage 3. This is a variable-created string.";
$one=4;
$two=5;
echo $text."<br />";
echo $one."x".$two."=".$one*$two."<p />";

// Part 4

function multiply($x,$y){
    $product=$x*$y;
    return $product;
    }
echo "Stage 4. The following statement is done by calling a function and returning the result of the function"."<br />";
echo "6 x 7 = ".multiply(6,7)."<p />";

echo "Stage 5. The following uses the \"include()\" function to include another php file. <br />";
include("external.php");

Place this PHP in external.php.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' ></meta>
<title>Untitled Document</title>
</head>

<body>
<p>Hey everyone this sentence is written in html in external.php</p>
<?php echo "hey everyone this sentence is writen in php in external.php." ?>
</body>
</html>

Return to top