PHP

From Redbrick Wiki
Revision as of 22:19, 8 January 2008 by Coconut (talk | contribs)
Jump to navigation Jump to search

This is a quick introduction to PHP, which will be expanded on (eventually).

What is PHP?

PHP Hyper-text Pre-Processor. Allows web developers to write dynamically generated pages quickly. With synergy!

PHP manual: http://ie.php.net/manual/en/

Basic PHP

  • cd to your ~/public_html folder on Redbrick.
cd ~/public_html
  • Create a file called “hello.php” using a text editor.
nano hello.php
  • Paste (or manually type, if you're a newb) this PHP code into the fine
#!/usr/local/bin/php
<?php
   echo “Hello World!”;
?>
  • Save and exit (CTRL + O, CTRL + X)
  • Set correct permissions for the php file (this must be done for every .php file you want to use).
chmod 700 hello.php
  • Visit www.redbrick.dcu.ie/~username/hello.php to view your script's output.
  • It should display “Hello World!” in plain text within your browser.

The include() function

Without understanding much about the details of PHP, you can save yourself a great deal of time with the use of the PHP include function. The include function takes a file name and simply inserts that file's contents into the script that calls the include function. (Ref: tizag.com)

menu.php

<a href="http://www.example.com/index.php">Home</a> -
<a href="http://www.example.com/about.php">About Us</a> -
<a href="http://www.example.com/links.php">Links</a> -
<a href="http://www.example.com/contact.php">Contact Us</a>

index.php

<html><body>
<?php include("menu.php"); ?>

Home page content

</body></html>

Where you inserted “include(“menu.php”);” the contents of menu.php are included into index.php at that location. When you add or remove pages from your website, you only need to update one file, menu.php, in order to display a link on every page in your website that includes menu.php.

Variables

Like any decent programming language, PHP uses variables.

PHP variables can store any data you wish. They start with a $ sign and contain alphanumerical characters, but must always start with a letter.

$example1 = “example”;
echo $example1;

This will output the text “example”.

Variables as the name suggest can be altered after they're declared.

$example1 = “altered example”;

$example1 now contains “altered example” and the previous contents are gone.