Go Back   Hardware Forums > Software Support Forums > Web Development

Reply
 
LinkBack Thread Tools
Old 09-02-2006, 09:58 PM   #1 (permalink) Top
Alpha Geek
 
Waffle's Avatar
 
Join Date: Apr 2004
Age: 20 Male
Posts: 2,270
Times Helpful: 51
My Mood: Angelic
Status: Offline

My Computer

Default PHP - Beginner Tutorial

This tutorial is for anyone who has never touched PHP coding before...if you have, you'll find it kind of basic, but I will guide you through installing, and creating several basic scripts to get a feel for the language.

It would help if you know HTML, but this is not necessary for this tutorial.

Step One
Firstly, you will need to obtain access to a PHP Capable server.

Don't fear, for if you do not have access to a suitable web server, for there are plenty of easy options for creating scripts offline.

If you don't have access, then head on over to Apache Friends and download yourself a copy of Xampp.

This brilliant program will fully install and configure PHP, MySQL and Apache Web Server on to your computer.

Download, install the program. Install MySQL and Apache as services.

Once installation is complete, point your browser over to:

http://localhost/

and if the installation was successful, you will see the Xampp welcome screen.

Choose your language, and you are ready to go. Navigate on the left - you can access phpMyAdmin, the internet standard for creating and managing SQL Databases, but for now, we aren't worried about that.

Step Two
You are ready to begin coding your first PHP script.

Open up notepad or dreamweaver, or essentially any text application.

Start a new document, and type in the following:

PHP Code:
<?php

echo "This is my first script!";

?>
Save the file as firstscript.php in this location:

C:\Program Files\xampp\xampp\htdocs

(I would suggest making a new folder for your scripts, so maybe:

C:\Program Files\xampp\xampp\htdocs\myphp\)

now, point your browser over to:

http://localhost/firstscript.php

or if you saved it in a folder..

http://localhost/myphp/firstscript.php

Hopefully, if all has gone well, you will see the following:


Code:
This is my first script!
if you don't or get an error..something went wrong in the installation of php.

If you do, however, then, move on.

Step Three
PHP Code is a server side scripting language...all the content is dynamic and so everything is carried out on the server before being sent to the browser, as HTML. This means that your code is safe from people trying to steal it, and it enables you to do some awesome things with websites and applications.

The basic syntax is:

PHP Code:
<?php  //START php
//PHP Code here
?> //END php
note php can be opened and closed many times within a document, it integrates very well with HTML and forms. You just must make sure the file is saved with a .php extension for the browser to parse php.
PHP Code:
<?
#this, is also a comment. it won't be displayed to the user. good for annotating code.

/* And this, is also

a comment, stretching

over multiple lines
*/
?>

//You can also start and end PHP by using

<? ?> //but this is informal and not XHTML compliant
note, you terminate every instruction in php with " ; ". There are some exceptions, but as a whole, every line of code will end in ;, else there will be a parse error.

Variables are a crucial part in any language, and PHP is no exception.

Variables are declared like this:

PHP Code:
<?

$variablename 
"variable value";

//note that variable names CANNOT start with an integer..they can start with letters or an underscore.

#also note that variable values must be contained in ""s unless it is an integer, in which case this is not necessary

$myname "Waffles";
$_myage 17;

?>
Some of the basic syntax in php are listed below. You've already looked at one of the most common, ECHO.

PHP Code:
<?php //start PHP

//function name

echo ("Something here"); //this function simply prints whatever text/variable/string was entered between the "'s and ().

//example usage:

echo "Hey, this is an echoed statement.";
//prints Hey, this is an echoed statement. to the browser

echo "$string";
//prints whatever value the variable string had

?>
Last thing for the tutorial for now, IF/Else statements.

These are vital for any application to have more than one possiblity, based on whether or not an arguement is true or false.

the strucutre for an IF/Else is:
PHP Code:
<?

if (something is true) {

// do something

} else {

// do something else

}

?>
in english this translates to:

If something is true/false (it can be either, or something more advanced such as if a value is present), then do something. Else, if this is not true, then execute something else.

It's easier in practice.

Load a new script:

PHP Code:
<?php

// IF/Else statement

//lets set up some variables..

$name "Waffles";

if(
$name == "Waffles") { //it is, so:

echo "My name is Waffles!";

} else {

echo 
"My name is not waffles!";

//end the statement.

?>
load that, and see what the result is.

You should be displayed with the first message.

Now go back to the script and change the name in $name, to some random crap.

Now run the script again. As the $name is no longer equal to (==) "Waffles", the other code is executed.



And there we have..very, very, basic PHP.

Send a message via MSN to Waffle   Reply With Quote
The Following 3 Users Say Thank You to Waffle For This Useful Post: Show me >>
Whats this? Ultra Flat Keyboard
Ultra Flat Keyboard
Seller Price (inc. VAT) Delivery Total Price Availability Seller Rating
Misco.co.uk £7.95 £2.34 £10.29 In Stock Rated: 4 out of 5 - Number of votes: 1351
Dell £17.90 Free £17.90 In Stock Rated: 0 out of 5 - Number of votes: 0
IT247.com £9.24 Free £9.24 In Stock Rated: 4 out of 5 - Number of votes: 38
Old 09-02-2006, 10:07 PM   #2 (permalink) Top
Administrator
 
Sniper's Avatar
 
Join Date: Oct 2001
Age: 24 Male
Posts: 4,380
Times Helpful: 114
My Mood: Meh
Status: Offline

My Computer

basic is where it all starts, good tutorial dude! I was wanting to write some php tutorials, since its something I could write about without...maybe after my exams.
  Reply With Quote
Old 10-02-2006, 08:20 AM   #3 (permalink) Top
The King

 
Addis's Avatar
 
Join Date: Jan 2004
Age: 18 Male
Posts: 5,254
Times Helpful: 402
My Mood: Drunk
Status: Offline

My Computer

Good tutorial. Carrying on from if statements...

Every statement should be terminated with a ;. Because the PHP interpreter program ignores whitespace characters during coding then it is important to remember to terminate every statement. e.g.:
PHP Code:
echo "Test..."$a $b//would be the same as
echo "Test...";
$a $b
Notice that the if (condition) does not terminate in a ;, as it compares an expression to see if its true or false. However because it often carries more than one argument you need to use the { and } around your true condition code to group them together.
__________________
Never trust a program you don't have the source code for.

My website | Powerful Desktop Linux | Linux for human beings | Linux for power users | Linux for ricers
Send a message via MSN to Addis   Reply With Quote
The Following 2 Users Say Thank You to Addis For This Useful Post: Show me >>
Old 10-02-2006, 08:31 AM   #4 (permalink) Top
Nonconformist Geek
 
Anti-Trend's Avatar
 
Join Date: Oct 2003
Age: 27 Male
Posts: 4,813
Times Helpful: 524
Status: Offline

My Computer

* Stickied *
Send a message via ICQ to Anti-Trend Send a message via AIM to Anti-Trend   Reply With Quote
Old 27-02-2006, 08:47 PM   #5 (permalink) Top
Alpha Geek
 
Waffle's Avatar
 
Join Date: Apr 2004
Age: 20 Male
Posts: 2,270
Times Helpful: 51
My Mood: Angelic
Status: Offline

My Computer

Time for an update..not so much beginners, but just random PHP Stuff.

Functions
Functions, are either:

Prebuilt into the code of a given language, and (usually), return values when parameters are passed into them.

They can also be defined by a programmer, to create useful shorthand pieces of code, that can be reused quickly and easily.

Some existing PHP Functions
At last count, some 3000+ functions are built into PHP (the latest version anyway). They are constantly being expanded, as new versions/releases are brought out.

A function is structured like this:
PHP Code:
<?

functionname
(parameters) {

code to be executed with given parameters

}
?>
Here are some of the more common functions.

echo() - is used to display output to the web browser.

It is not strictly a function however, it is a language construct. It cannot return a value, but is considered a faster operation than the alternative, Print.

Print works largely the same as Echo, except Print can return a value ( True/False).

It is also said to be slower in execution, however the difference is 1000's of miniseconds, and so this is barely worth mentioning.

Both will return the data entered between the parentheses to the browser (although echo does not require the ()'s, it will operate with just quotation marks).

PHP Code:
<?

echo "Hello!"// shows "Hello!" on the screen
print ("Hello!"); //shows "Hello!" on the screen

?>
Also to note, the parentheses in this example are not vital, but is considered valid coding.

-------------------
strtolower($string); - self explanatory - String to Lower (case), presumably.

example usage:
PHP Code:
<?php
$str 
"Waffles AND SyRuP go Well TogetHer";

$str strtolower($str);

echo 
$str// Prints waffles and syrup go well togther
?>
note that the string ($str), was passed into the function strtolower($str), and the new value of $str, after being stripped of capital letters, is then echoed.

I believe this is recursive coding...using the same variable again, but in like an updated form. (vague explanation admittedly)

------------

Now, to create your own function..

First you need to tell PHP you are starting a function, then give it a name:

PHP Code:
<?

function MyFunction() { }

?>
is the syntax.

You must then decide what you want your function to do.

I have coded a handy function, that allows the coder to enter the name of a database, and a table within, and have the number of rows returned to the screen (this is useful when counting say, the number of members, or tutorials a database has.)

Rather than type out lengthy SQL queries, several times, the function can be used multiple times, with different table/database names to return the row count.

I will call the function "CountRows", and I need only 2 variables (it's basic).

that would be, the database name, and the table name.

PHP Code:
<?
function CountRows($database,$table) {}
?>
we need to do something with those variables..so:

PHP Code:
<?
function CountRows($database,$table) {

mysql_connect("localhost","user","pass"); //connect to mysql

mysql_select_db($database); //select the database entered into the first parameter..

//run a query on the chosen table..

$sql "SELECT * FROM $table";
$result mysql_query($sql);
//now count the rows..
$num mysql_num_rows($result);

return 
$num;

mysql_close() //close the connection
}  //and end function
?>
we are done.


in order to use this, we would first include the function code on the page we are working, and then:

PHP Code:
<?

include "functions.php"//includes our functions page, 
//so we can use them easily.

//then..

echo "Number of downloads:"//we want to count our downloads.

echo CountRows(data,downloads);

//data is the database name, dowloads is the table name. 
//this will return the number of rows in the table, and output it as an integer.

?>
and there you have, a basic, very useful, working function.

I would probably extend this by throwing in some error handling, like what happens if the user can't connect to MYSQL or chooses an invalid table/db name. But providing you enter the correct info, you will be able to count any row, in any table, in any database.

Hope this helps.
Send a message via MSN to Waffle   Reply With Quote
The Following 3 Users Say Thank You to Waffle For This Useful Post: Show me >>
Old 08-03-2008, 01:58 AM   #6 (permalink) Top
Geek Trainee
 
Sn1P3R's Avatar
 
Join Date: Jan 2008
Gender: Male
Posts: 39
Status: Offline
???? OK, now im confused ????
  Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner with AC97 problems bazza Sound Cards and Speakers 2 24-04-2005 11:06 AM
I'm a beginner when it comes to computers steveaslin General Hardware 2 18-04-2005 08:51 AM
Sig Tutorial Waffle Tutorials 21 10-12-2004 09:23 PM
SATA help for a beginner mattymeik Storage Devices 1 06-10-2004 02:08 PM


All times are GMT +1. The time now is 10:10 AM.


Copyright © 2000 - 2008 · HARDWAREFORUMS.COM · All rights reserved