PHP for Noobs - Basic Module

This is a discussion on "PHP for Noobs - Basic Module" within the PHP Forum section. This forum, and the thread "PHP for Noobs - Basic Module are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 30th, 2003, 12:04
Reputable Member
Join Date: Aug 2003
Location: Singapore
Posts: 321
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gwx03
PHP for Noobs - Basic Module

<font size="5">Basic PHP Tutorial
For Newbies, By A Newbie</font id="size5">

At the time of writing, I’m pretty new in PHP but I guess it would be great if I could cut down all the junk in those PHP books and go straight to the point… This is a straightforward tutorial and covers pure PHP4 - basic.

This tutorial covers :

- Starting and Ending PHP statements
- print() function
- Classic Helloworld
- HTML and PHP
- Commenting
- Define and Accessing Variables
- Referencing variables
- Data types
- Changing data types
- Changing types by casting
- Assignment operator
- Math operators
- Comparison operators
- Incrementing, Decrementing
- Constants, defining
- Predefined Constants



Starting and Ending PHP Statements

PHP statements are started and ended using the start tag “<?php” and end tag “?>”. There are many other ways for telling PHP where to start and stop.

The following are some methods in which we can acheive the same effect.

<SCRIPT LANGUAGE="php"> YOUR CODE HERE </SCRIPT>
<% YOUR CODE HERE %>
<? YOUR CODE HERE ?>

Classic : Hello World!

The classic Helloworld in PHP

<?php
print(“Hello World!”);
?>

Lets just run thru this code. The start and end tags are on the first and last lines. In between is a function, called ‘print()’.

Function : Command that does something ( performs something ). Data sent to a function is placed in parentheses after the function name.

print() is a function that outputs data. The quotation marks, as you can see, means that the stuff inside is a string.

Quik note : The parenthesis are not needed only for print(). In other functions, that is needed! echo is another function which does the job of print!

String : A line of data or characters.

PHP doesn’t care if you use single or double quotation marks.

The brackets that contain “Hello World!” are required by functions. That is what we call PHP syntax, or the ‘grammar’ of PHP.

The semicolon informs the interpreter that you have completed a statement

Statement : Instruction to the interpreter


HTML and PHP


To use PHP in HTML, all you need to do is to do some PHP code and insert it into the HTML with the start and end tags. <?php and ?>

A Sample hello world PHP Script is show below
<html>
<head>
<title> PHP and HTML </title>
</head>
<body>

<?php

print (“Hello World”);

?>

</body>
</html>

What the code above does is to print hello world in italics.



Comments!

Comments are ignored by the interpreter and are used to know what a bloc of code does ( to tell a fellow programmer or refresh your memory )-

Single line comments begin with two forward slashes

//this is a comment

Multiline comments begin with a forward slash then an asterisk and end likewise.

/*
this is a
multiline
comment
The interpreter
doesn’t read this!
*/



Variables

Variable : Special container to store a value. Variables consist of a name that YOU choose preceded by a dollar ($) sign. The variable name can include anything, even the underscore character. Variable names cannot include spaces or characters that are not letters or numbers. The variable below is a ‘legal’ or correct variable. [ Note : Variables can't start with a number! ]

$vAr23_xyZ

The variable as PHP sees it is vAr23_xyZ.
Variables can hold numbers, strings of characters, objects, arrays or Booleans.

To use a variable, we must create ( or declare ) them. This can be time by using the assignment operator.

Operator : A punctuation sign such as the assignment operator (=)

To declare or variable vAr23_xyZ, we use :

$vAr23_xyZ=1000;

This assigns our variable with the string 1000.

If we were to tell PHP to print out what our variable vAr23_xyZ contains, we can just use our print function…

print ("$vAr23_xyZ")

What will happen is that PHP will automatically find out the value of our variable and display “1000” on screen.

Variables can be assigned to another variable. You can assign $firstVariable to $anotherVariable. When you do so, a copy of the value held in $firstVariable is stored in $anotherVariable. Subsequently changing the value of $firstVariable will have no effect on the contents of $anotherVariable.

In PHP 4, you can change this by forcing a reference to $firstVariable to be assigned to $anotherVariable instead of a copy of its contents by using the ‘&’ character. Placing a ‘&’ in front of $firstVariable fixes a reference to $anotherVariable.

The code is below :

$firstVariable=123;

// lets say we want to assign $anotherVariable the value of $firstVariable

$anotherVariable=&$firstVariable;

// first variable’s content would be altered…

$firstVariable=746;

//lets issue a print to see what happens to the value of anotherVariable

print ("$anotherVariable");

// print would output 746




Data Types

PHP will calculate data types as data is assigned to each variable. This would mean that variables can be used flexibly, holding a string at one point, and a integer at another.

There are 6 datatypes available : integer, double, string, Boolean, object and array

Let us look at how we assign data types to variables.

$mynumber=5; // assigns integer ‘5’, a whole number

$mydecimal=0.123; // assigns a floating point number ‘0.123’

$myStrInG=helloyourself; // assigns the collection of
characters, ‘helloyourself’

$boolean =true; // assigns a Boolean value of ‘true’

We’re looking at Object and Array later. Let us look at the definitions of integer, double, string and Boolean.

Integer – Whole or real number. Number with no decimal point
String – Collection of characters. They should be surrounded by quotation marks.
Double- Floating point number… It simply means that it is a decimal.. Float is geek talk
Boolean- True or false.. don’t know why they give such a weird geeky name

You can use the settype() function to change the type of a variable. Let us change a double into a boolean…

<?php

$vartypecanbechanged=1;
settype ($vartypecanbechanged, boolean );

?>

What will essentially happen is that the variable $vartypecanbechanged will be converted into an Boolean data type. Any number other than 0 becomes true when converted to a Boolean. Thus, our string will result in a true Boolean value.

We can change data types also by casting. By placing the name of a data type in brackets in front of a variable, we can create a copy of the variable’s value converted to the data type specified. Casting produces a copy, leaving the original variable untouched.

Take a look at the following example, which will create a new double variable $newvar from the orginal $original.

$original = 1.11;
$newvar = ( double )$original;






Operators and Expressions

Let us take a look at the terminology…

Operator : Symbol or series of symbols that when used in conjuction with values perform an action and usually produces a new value.

Operand : Value used in conjunction with an operator. There are usually two operands to one operator.

Expression : Any combination of functions, values and operators that resolve to a value.

Let us combine two operands ( 1 and 2 ) with an operator (+) to produce a new value!

1+2

( 1 + 2 ) is an expression. 1 + 2 results in 3. An expression need not contain an operator, though. Expressions can be ‘123’, without any operators…



Assignment Operator (=)

The assignment operator assigns a value to its left hand operand. For example :

$author=”gwx”;

The variable author is given the value of “gwx” using the assignment operator! The variable $author now contains the string “gwx”. This is a expression! A statement that uses the assignment operator always resolves to a copy of the value of the right operand.

Thus,

print (" $author = “gwx” ");

Prints ‘gwx’ to the browser in addition to assigning “gwx” to $author.



Mathematical Operators

Mathematical operators do math stuff. The addition operator adds the operands, the subtraction operator subtracts and so on…

A full list of math operators are the addition (+) operator, the subtraction (-) operator, the (/)division operator, the Multiplication (*) operator and the (%) Modulus operator.

Comparison Operator (< and > and = = )

Comparison operators perform tests on their operands. They return the Boolean value true or false. This type of expression is useful in writing control structures such as if and while statements.

Lets take for instance :

$x<2;

This statement would test whether the value of $x is smaller than 2. If x is larger than 2, the expression would resolve to false. However, if x is smaller than 2, then it would resolve to true.

Other comparison operators …

Operator Name Returns True If

== Equivalence Left is equal to right
!= Not Equal Left is not equal to right
=== identical Left=right and they are same
> Greater Left greater than right
< Smaller Left less than right
>= Greater or equal to Left greater than or
equal to right
<= Less than or equal to Left is less or = to right




Incrementing and Decrementing Integer Variable

The post increment operator consists of two plus signs appended to a variable name.

$x++ ;// $x is incremented

The post-decrement operator consists of two minus signs appended to a variable name.

$x--; // $x is decremented

When using post-increment or post-decrement operators in conjunction with conditional operators, do take note that the operand will only be modified after the test is complete.

$x = 3;
$x++ < 4; // results in true

In some cases, you might want to increment or decrement a variable in a test expression before the test is carried out. PHP provides the pre-increment and pre-decrement operators. They are written with the plus or minus symbols preceding your variable.

++$x; // increments $x
--$x; // decrements $x
Therefore, if we were to make use of these pre-increment/decrement operators…

$x=3;
++$x<4; // false




Constants

Variables offer a flexible way of storing data. You can change their values and type at any time. If you want to work with a value that you do not want to alter throughout your script’s execution, you can define a constant. There are 2 constant datatypes, a number or a string. The name of the constant should be capitalized.

Constants are accessed with the constant name. No $ sign is required. Lets define a constant, “MYFIRSTCONSTANT” and access it.

<?php

define ( “MYFIRSTCONSTANT”, “myvalue”);
print (“ Your constant is“.MYFIRSTCONSTANT);

?>

Do note that we used the concatenation operator to append the value held by our constant MYFIRSTCONSTANT to the string “Your constant is”. This is because the interpreter cannot differentiate between a constant and a string within quotation marks.




Predefined Constants

PHP automatically provides some built-in constants for you. _FILE_, for instance, returns the name of the file currently being read by the interpreter. _LINE_ returns the line number of the file. These are handy for debugging code.





Next Steps

In the ‘Intermediate’ module, we will be using what we have learnt here, in addition to the following areas of PHP :-

- Conditional Statements
- Functions
- Arguments/Parameters
- Arrays
- Objects

We shall also go in depth to certain areas of this tutorial, such as precedence, dynamic variables and much more. Hope to see you then!

Closed Thread

Tags
php, noobs, basic, module

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
review module page numbering help fr34k123 PHP Forum 0 Aug 14th, 2007 15:16
maker/checker module kumarkbn Classic ASP 3 Apr 20th, 2007 15:18
Problem with PHP install – ‘module already loaded’? Tim356 PHP Forum 0 Jan 19th, 2007 02:44
Maker/Checker Module EmanIT ASP.NET Forum 0 Apr 23rd, 2006 07:11


All times are GMT. The time now is 21:33.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43