School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,216 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,577 people online right now. Registration is fast and FREE... Join Now!




Beginning PHP

 
Reply to this topicStart new topic

> Beginning PHP

Rating  4
kewlkreator
Group Icon



post 12 Aug, 2009 - 07:02 AM
Post #1


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:
CODE
<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:
CODE
<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:
CODE
<?php
$varName = value;
?>

When calling a variable, use:
CODE
<?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:
CODE
<?php
$hello = "Hello";
$world = "World";
$fullLine = $hello . " " . $world;
echo "$fullLine";
?>


Comments
Comments in PHP are written just like in java script:
CODE
/*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! smile.gif

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:
CODE
<?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
CODE
<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:
CODE
<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:
CODE
<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! smile.gif smile.gif

Instalation: I take no responsibilty for what you download here or on apache friends. That is your decision.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

traffy
*



post 2 Nov, 2009 - 12:48 AM
Post #2
Hi!
Thanks for your tutorial!
I think in lesson:If...Else,
after the line:echo "Number is not 10", we should have a ";"
Go to the top of the page
+Quote Post

AbuJaFaR
***



post 2 Nov, 2009 - 01:32 AM
Post #3
Nice tutorial. icon_up.gif
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.gif
Go to the top of the page
+Quote Post

no2pencil
Group Icon



post 2 Nov, 2009 - 02:14 AM
Post #4
QUOTE(AbuJaFaR @ 2 Nov, 2009 - 03:32 AM) *

Could you explain with more details between post and get?

Get & Post Methods: How to & why
Go to the top of the page
+Quote Post

AbuJaFaR
***



post 2 Nov, 2009 - 02:16 AM
Post #5
QUOTE(no2pencil @ 2 Nov, 2009 - 02:14 AM) *

QUOTE(AbuJaFaR @ 2 Nov, 2009 - 03:32 AM) *

Could you explain with more details between post and get?

Get & Post Methods: How to & why


Cool, thanks! biggrin.gif
Go to the top of the page
+Quote Post

ladyinblack
Group Icon



post 2 Nov, 2009 - 03:37 AM
Post #6
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: 2 Nov, 2009 - 03:38 AM
Go to the top of the page
+Quote Post

ladyinblack
Group Icon



post 2 Nov, 2009 - 04:26 AM
Post #7
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: 2 Nov, 2009 - 04:58 AM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 10:06PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month