Jump to content
xisto Community
ghostrider

Basic Php Tutorial

Recommended Posts

Hypertext Preprocessor, or PHP for short, is a dynamic language, meaning that unlike HTML, PHP can change itself without you having to update the page using FTP or the CPanel. PHP has endless possibilites, you can use MySQL (a database program) with it, create polls, counters, work with images, process forms and CGI, and even send email. In order for you to start PHP coding, you need to tell the server that is processing your webpage that this is PHP. You can do that 3 seperate ways.

<script LANGUAGE="PHP">
<?PHP
<?

After you have done that, you can start writing in PHP. The first function that you will learn is echo().
Echo() will output a string of text that you specify. Here is an example:
<?PHPecho("Hello World!");PHP?>

Each line of code in PHP ends with a semicolon (;). This is very important, if you don't do this, your program will crash with a fatal error.

Another way you can do the same thing is like this:
<?PHP$thisisavariable = "Hello World!";echo("$thisisavariable");PHP?>

$thisisavariable is a variable. All variables have a dollar sign ($) before them. Variables can also contain numbers, as shown below.
<?PHP$var = 123;PHP?>

Comments can be shown in PHP also, they are placed after semicolons and have "//" before them.
<?PHP$var = 123; // Put the number 123 in the variable $var.PHP?>

Below is an example of what you can do with PHP. It will be commented so you can follow along.
This is how it works:
Check to see if the user has specified their name. If not, display a form so they can enter there name. If they have entered there name print "Hello, (user's name here)!".
<?PHP //Tell the server that to process the PHP.if ($_GET['name'] <> "")  //If statements do not need semicolons.{ //This just means this is where the code for the if statement starts.// This code is run if they have specified their name.  The <> means not equal.$user_name = $_GET['name']; //Put the user's name is $user_name.echo("Hello, $user_name");}else // This means that the user did not specify a name yet.{echo("Please type your name and press Submit:");echo("<FORM ACTION='3.php' METHOD='GET'>");//Notice I am using single quotes inside of echo.  If you were to use double quotes you would crash your program.echo("<INPUT TYPE='text' NAME='name' LENGTH='30'>");echo("<INPUT TYPE='Submit' VALUE='Submit'>");}PHP?>

I really hoped this helps some people start to learn PHP. Any questions, comments or constructive critiscism (this is my first tutorial) can be posted and I will answer them. I'll post the links to the 3 examples (Hello World, Hello World (with a variable), and the example once I get enough credits to become active again :).

Share this post


Link to post
Share on other sites

Well, i want to colaborate with this post so i expose lil thingz related to simple maths to be used in PHP:

Operators are the common on most of programming languages, * for multiplying (3x2 = 3*2), + for sum (3+2 = 5), - for resting (3-2 = 1), / for division (3/2=1.5), and ^ for exponentials (3^2 = 9).

So if you want to sum some stored variables (of a DB, or whatever you want/need :)) you could do the next:

<?$var1= '3';  // that could come from a form if you replace the '3' string with a $_GET[''] or $_POST['']$var2= '2';  // ... the same :Psettype($var1, 'integer');  // That makes a string into an integer, i.e. '5a' it will be converted into the number 5.settype($var2, 'integer');  // The same for the $var2/* You could ask why didnt i wrote "$var1=3;" instead of "$var1='3'", i did that cuz in the forms most of times is sent as a string, and the ppl could write 5a, 3v, c, so the settype makes the string to an integer (the integers dont have ' for delimitating) */$sum = $var1+$var2;echo $var1.' and '.$var2.' equals to '.$sum; // You'll have to get something like "3 and 2 equals to 5"?>

It was a micro help, but from that, you could figure out to use -, *, / and ^ hehe.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.