PHP: Difference between revisions
mNo edit summary |
(Remove legacy information.) |
||
| (6 intermediate revisions by one other user not shown) | |||
| Line 14: | Line 14: | ||
cd ~/public_html | cd ~/public_html | ||
* Create a file called | * Create a file called ''hello.php'' using a text editor. | ||
nano hello.php | nano hello.php | ||
* | * Type this PHP code into the file | ||
<?php | <?php | ||
echo | echo "Hello World!"; | ||
?> | ?> | ||
| Line 48: | Line 47: | ||
<html><body> | <html><body> | ||
<?php include("menu.php"); ?> | <?php include("menu.php"); ?> | ||
</body></html> | </body></html> | ||
| Line 59: | Line 57: | ||
PHP variables can store any data you wish. They start with a $ sign and contain alphanumerical characters, but must always start with a letter. | 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 = | $example1 = "example"; | ||
echo $example1; | echo $example1; | ||
This will output the text | This will output the text "example". | ||
Variables as the name suggest can be altered after they're declared. | Variables as the name suggest can be altered after they're declared. | ||
$example1 = | $example1 = "altered example"; | ||
$example1 now contains | $example1 now contains "altered example" and the previous contents are gone. | ||
[[Category:Helpdesk]] | [[Category:Helpdesk]] | ||
Latest revision as of 20:45, 28 September 2010
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
- Type this PHP code into the file
<?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"); ?>
</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.