Page 1 of 1

Beginning PHP Rate Topic: ****- 1 Votes

#1 kewlkreator  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 38
  • View blog
  • Posts: 1,065
  • Joined: 25-March 09

Post icon  Posted 12 August 2009 - 08:02 AM

Beginning PHP
Hello! In this tutorial, I'll show you how to start learning PHP. By now, you should have some basic knowlege of HTML/XHTML.

Get Started
PHP files end with *.php. However, to run these files in a browser, you need to set up a localhost. I did this by installing XAMPP from here. Once you have it installed, navigate to C:/xampp/htdocs. Place a test file called test.html and pull up your browser. Go to http://localhost/test.html and you'll find your file! Success, XAMPP is installed.
NOTE: By installing XAMPP, you allow users to navigate there by knowing your IP address. To stop this, go to http://localhost/ and click on "Security" then follow the instructions.

Your first PHP
Everyone starts with the "Hello World" application, lets follow tradition! Save this as helloworld.php:
<html>
<head>
<title>Hello!</title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>

Now you must be wondering why we used HTML elements in a PHP file. Run the file at http://localhost/helloworld.php . You should see "Hello World" on the screen. Now, to see the advantage of PHP, view the source:
<html>
<head>
<title>Hello!</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>

There is the advantage of PHP! Since PHP is a server-side language, it processes the code before the client sees it. This is why PHP is so widely used in secure systems.
Taking the code apart, the <?php starts PHP code, the echo " "; command prints a line, and the ?> code closes the PHP code.
NOTE: PHP code will not work with the *.html or *.htm extension. A PHP file must have the *.php extension.

Variables
As you would know if you know Javascript, variables are "code that can vary". Use them to work with loops, if-else's, and others. Variables can also keep you from typing code over and over. PHP variables are declared like:
<?php
$varName = value;
?>

When calling a variable, use:
<?php
$var1 = "Hello";
$var2 = "World";
$total = $var1 . " " . $var2;
?>

Pay close attention to $total and you'll notice the period that adds the strings. This is called a concentration operator and adds two or more strings. Also see how we called $var1 and $var2 with a $ symbol just like how we declared them.

Another form of our "Hello World" app:
<?php
$hello = "Hello";
$world = "World";
$fullLine = $hello . " " . $world;
echo "$fullLine";
?>


Comments
Comments in PHP are written just like in java script:
/*This is a 
multi-line
comment */

// This is a single line comment
# So is this

Comments are not executed nor seen by the processor. They are for your eyes only! :)

Operators
Operators are what shuffle and add and subtract variables, strings, etc. A list of operators:
  • = : Assignment
  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Modulus
  • . : String concentration
  • == : Equal to
  • === : Equal to and of same type
  • <> or != : Not equal to
  • !== : Not equal to; not same type
  • < : Less than
  • <= : Less than or equal to
  • > : Greater than
  • >= : Greater than or equal to
  • && : Logical AND
  • || : Logical OR
  • xor : Logical XOR
  • ! : Logical NOT
  • ++ : Add 1
  • -- : Subtract 1
Some of these operators you will have seen in math such as "+" or "-". Others might be unfamiliar. Operators will come in handy in the next lesson:

If...Else
If else is the most used statement in just about all code. And example is shown:
<?php
$number = 10;
if ($number == 10) {
echo "Number is 10";
} else {
echo "Number is not 10"
}
?>

See how we used { and } to define what the commands should do if the tests returned true or false. Now is where variables come into handy. Say you have input from a user. You can assign that input to a variable and run tests on the variable.

Data from forms
This is the most advanced idea in the whole tutorial so fear not! Lets say you have a form in HTML
<html>
<body>
<form method="post" action="actionhere">
<input type="text" name="name" />
<input type="submit" />
</form>
</body>
</html>

See how the form element has an action=" parameter? Fill this in:
<form method="post" action="action.php">

This will redirect the user to "action.php" when the user clicks submit. To get information from a form, we use:
<html>
<body>
<?php
$name = $_POST["name"];
echo "Your name is: " . $name;
?>
</body>
</html>

Here we used $_POST["input"] to grab the input from the form. Then we assigned it to a variable and wrote it to the screen. You can also us $_GET if the method of the form was get. Get passes the input through the address bar and can be handy if the user want to bookmark the page.

?>
Ok, hopefull, I've given you the basics of PHP! Go have fun! :) :)

Instalation: I take no responsibilty for what you download here or on apache friends. That is your decision.

Is This A Good Question/Topic? 0
  • +

Replies To: Beginning PHP

#2 traffy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-November 09

Posted 02 November 2009 - 01:48 AM

Hi!
Thanks for your tutorial!
I think in lesson:If...Else,
after the line:echo "Number is not 10", we should have a ";"
Was This Post Helpful? 0
  • +
  • -

#3 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Posted 02 November 2009 - 02:32 AM

Nice tutorial. :^:
Could you explain with more details between post and get?
I thought get just shows the input in the address bar and post does something like a hashcode that shows in the address bar.
Am I wrong? :blink:
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,423
  • Joined: 10-May 07

Posted 02 November 2009 - 03:14 AM

View PostAbuJaFaR, on 2 Nov, 2009 - 03:32 AM, said:

Could you explain with more details between post and get?

Get & Post Methods: How to & why
Was This Post Helpful? 0
  • +
  • -

#5 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Posted 02 November 2009 - 03:16 AM

View Postno2pencil, on 2 Nov, 2009 - 02:14 AM, said:

View PostAbuJaFaR, on 2 Nov, 2009 - 03:32 AM, said:

Could you explain with more details between post and get?

Get & Post Methods: How to & why


Cool, thanks! :D
Was This Post Helpful? 0
  • +
  • -

#6 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Posted 02 November 2009 - 04:37 AM

Quote

Pay close attention to $total and you'll notice the period that adds the strings. This is called a concentration operator and adds two or more strings. Also see how we called $var1 and $var2 with a $ symbol just like how we declared them.


Forgive me if I'm wrong, but shouldn't the word be concatenation operator. I'm a total beginner with php but I know this much with regards to programming.

Overall cool tutorial.

This post has been edited by ladyinblack: 02 November 2009 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#7 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Posted 02 November 2009 - 05:26 AM

Ok, so I downloaded and tried but it not working. I do the first step, creating a test file, but its just not loading up. Saying "Internet Explorer cannot display the webpage"

EDIT: So I figured it out.

This post has been edited by ladyinblack: 02 November 2009 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

#8 3G Alarm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 31-May 09

Posted 11 January 2010 - 01:19 AM

am new to php nice to see tutorial for beginners thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1