[PHP] PHP For Starters

Complex

New Member
Messages
18
Reaction score
0
Points
0
PHP: For Starters
This tutorial assumes that you have a basic knowledge of programming. No PHP Knowledge required.

I. Basic Syntax

All PHP Scripts start with one of 3 things: <?php or <? or <%
and End with one of 2 things: ?> or %>
NOTE: <% and %> only works if ASP tags are enabled in the server's php.ini! <?php is recommended over all.

Example:

PHP:
<?php
//PHP!
?>

PHP:
<?
//PHP!
?>

PHP:
<%
//PHP! Deprecated
%>

You may have noticed the text prefixed with "//". This is a comment in php. Comments are ignored by the compiler and are used to help the developer understand the code. Comments in PHP can follow 3 things:

//comment - A normal one line comment
# Comment - Another one line comment
/* COMMENT!
YEP */ - A multiline comment

Example:

PHP:
<?php
//Comment 1
#Comment 2
/* This is a
multiline
comment. All this is
ignored
*/
?>

icon4.gif
IMPORTANT! - All lines of code in PHP MUST END WITH a semicolon. You'll see more examples of this later. The only things that do NOT end with a semicolor are the opening and closing tags and comments

II. Variables

A variable in PHP is simple. It starts with a dollar sign ($) followed by a lowercase or uppercase letter or an underscore ($V or $v or $_v) and can contain an underscore and numbers. Here are some exampes:

$var - GOOD
$var2 - GOOD
$v_ar - GOOD
$2var - BAD
$hÜh - GOOD

Also, variable names are case sensitive so:

PHP:
$var = "Hi!";
$Var = "Bye!";

You would think that "Bye!" is now in $var, but its not. $var is still "Hi!" since $Var has a capital V, so its different. Be careful when writing a script about this. A good remedy is to not use similar names for variables and make it all lowercase.

icon4.gif
NOTE: One of the wonders of PHP is that you dont have to worry about types. A variable can be an int, string, bool, array and more without you having to define it as one.

III. Math

Math in PHP is just like in other languages:

+ Add
- Subtract
* Multiply
/ Divide
% Modulus

Very very simple. Examples:

PHP:
<?php
$addexample = 5 + 4; //Add. Sum = 9
$subtract = $addexample - 4; //$subtract = 5
$multiply = $subtract * 2 //$multiply = 10
$divide = $multiply / 2 //$divide = 5
$mod = $divide % 2 //$mod = 1
?>

Simple huh?

Functions

Functions are again, very easy, in PHP. Here is the syntax of one:

PHP:
function funcName($params = $defaultVal) {
//Code
}

Lets start from the beginning on this one. Before defining any function, you must put "function " in front of it to show that it is one. Next comes the name of the function, you will use this name to call it in your code later. After that comes parenthesis and inside you can optionally put parameters. If after a param you put "= value" where value can be anything from a number to a string to a boolean value, this will be the default value if none is supplied. Otherwise if there is no default value and the function is called and no value is given for that parameter, PHP gives a fatal error. Here is a working example using a function:

PHP:
<?php
//Pythagoreus theorem:
function thmPythag($a, $b) {
    return sqrt($a^2 + $b^2);
}

$num1 = 3;
$num2 = 4;

$hypot = thmPythag($num1, $num2); //$hypot = 5
?>

In the above example, 2 numbers are given to the function thmPythag which uses the Pythagoreus theorem to give the length of the hypotenuse of a right triangle. You may notice a couple things you havent learned yet:

return and sqrt.

Sqrt is just a built in function in PHP to find the square root of a float.
Return is used in a function to return a value! In VB its like this:

Code:
Function myFunc() as Integer
myFunc = 5
End Function

In PHP:

PHP:
function myFunc() {
return 5;
}

IV. Scope

Have you ever heard of the term Scope? Local Scope? Global Scope? Well if not, I am going to explain them now. Whenever you init a variable, it is put into LOCAL scope. This means that it can only be used in that scope. A scope is defined using brackets {}. Example:

PHP:
<?php
$var = "hi!";

function scope() {
$myvar = $var;
return $myvar;
}
?>

If scope() was called, the return value would be blank. Why? Because $var was initialized outside of scope()'s scope. To use $var, you would have to put it into GLOBAL scope like so:

PHP:
<?php
$var = "hi!";

function scope() {
global $var;
$myvar = $var;
return $myvar;
}
?>

Now if you called scope(), it would return "hi!" That is a very basic example of scope, I may make a more detailed and advanced scope tutorial somewhere down the road, but for your sake, the above will do, for now.

V. Control Structures
i. If Structure

An if structure will compare an expression and if it results in TRUE, it'll execute the code following it, otherwise, it'll skip it. Syntax:

PHP:
if (expression)
    code

This is good for one line of code, if you need your code to span multiply lines after an if, use brackets:

PHP:
if (expression) {
    code
    morecode
}

You can use many operators in the expression area of an if statement:

> - Greater than
< - Less than
>= - Greater than or equal to
<= - Less than or equal to
== - equal to
!= - not equal to
! - not
&& - And
|| - OR
^ - XOR

icon4.gif
NOTE: In PHP, = is not the same as ==. What = tells PHP is "Set the value of the right side to the left side" and == tells PHP "Compare the value of the left side with the right side, if they are the same, return TRUE, else, return FALSE"

Here are examples of each operator:

PHP:
<?php
$num1 = 5;
$num2 = 10;

if ($num1 > $num2)
    //Greater

if ($num1 < $num2)
    //Less than

if ($num1 >= $num2)
   //Greater than or equal to

if ($num1 <= $num2)
   //Less than or equal to

if ($num1 == $num2)
  //Both the same

if ($num1 != $num2)
  //Not the same

if (!$num1) {
  //$num1 = True (anything not 0)

if ($num1 && $num2) {
  //Both are true

if (($num1 > $num2) || ($num1 == $num2))
  //Greater than or equal to (not using >=)

?>
 

Maurice

New Member
Messages
1,930
Reaction score
0
Points
0
Re: PHP: Starters

Auvee said:
True, but I won't say anything bad, as I'm sure this will help out many people who are new to PHP.

-Auvee B.

Ofcourse it will, I think every PHP coder (New) has too start with this. :homestar:
 

Conquester777

New Member
Messages
180
Reaction score
0
Points
0
Re: PHP: Starters

yeah really, if you're gonna copy from some other place; even if it's good. give credit, you've already copy+pasted all they wrote, can't you copy+paste the url too?
 

wonkothesane

New Member
Messages
84
Reaction score
0
Points
0
Hey, I liked it. Very basics and good for people starting out with php (me, lol). Reminds me a lot of java (only other language i know... a little). Just one question, can you use php modularly like you can java? thnx
 

MicrotechXP

New Member
Messages
7,644
Reaction score
0
Points
0
Very Helpfull. I am going to start reading now becasue I wanted to learn php.
 

alec.t

New Member
Messages
10
Reaction score
0
Points
0
Great info, pitty you meshioned using globals in the demo..
is there any info that tells you how to do it without using globals
thanks
 

cHaRuS

New Member
Messages
44
Reaction score
0
Points
0
Thank You Very Much, i've started looking for this, i have a friend who needs this, as i'm not the right one to explain... :p
 

faith

New Member
Messages
6
Reaction score
0
Points
0
wow thanks thats tutorials is really help me get along with PHP
 
Top