Difference between revisions of "PHP essentials"

From Joomla! Documentation

Line 141: Line 141:
 
The  following are general syntax of the control structures
 
The  following are general syntax of the control structures
  
<pre>
+
 
 
'''if statements'''<br>  
 
'''if statements'''<br>  
if( statement) {
+
<source lang="php">
code
+
if( $x > 0) {
}  else if( statement) {
+
print("$x is positive");
code
+
}  else if( $x < 0 ) {
 +
print("$x is negative");
 
} else {
 
} else {
 +
print("$x is 0");
 
}
 
}
 +
</source>
  
 
'''for loop'''<br>  
 
'''for loop'''<br>  
 
for ( $x=1; $x<= 10; $x++ ) {
 
for ( $x=1; $x<= 10; $x++ ) {
  echo "$x<BR/>";
+
  print "$x<BR/>";
 
}
 
}
  
Line 166: Line 169:
 
}while($x <= 10);
 
}while($x <= 10);
 
</pre>
 
</pre>
 +
 
==== Defining functions ====
 
==== Defining functions ====
  

Revision as of 16:07, 8 January 2009

Quill icon.png
Page Actively Being Edited!

This article is actively undergoing a major edit for a short while.
As a courtesy, please do not edit this page while this message is displayed. The user who added this notice will be listed in the page history. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page. If this page has not been edited for several hours, please remove this template, or replace it with {{underconstruction}} or {{incomplete}}.

PHP essentials[edit]


Many web designers and casual website owners are not conversant with the PHP language in which Joomla! is written and since Joomla! templates usually contain PHP statements it is necessary to understand at least a little of the language in order to be able to create or customise templates. This chunk will describe, in simple terms, how to use each aspect of PHP in the context of a Joomla! template. For example, a simple explanation of the PHP if-then-else syntax using extracts from a template for illustration.

The best resource to learn PHP is probably hands on experience and Joomla can provide that to you thanks to it's native PHP code. This can be overwhelming for people who have not programmed before. Though Joomla is easy enough to install with the help of the community and a little of your time you'll have people asking you to do their websites.

One place that has a nice short list if functions and examples is http://en.wikiversity.org/wiki/25_Essential_PHP_Functions which is good to get familiar with PHP. Though for an extensive function list, explanations, and examples go to http://www.php.net.

Once you have the basics of PHP you'll need to get the basics of the Joomla PHP syntax. Start by looking at some existing extensions and check out the code and also take a look at some these Tutorials about Joomla:

How to create a Joomla! Plugin

Developing a Model-View-Controller Component - Part 1

Developing a Model-View-Controller Component - Part 2 - Adding a Model

Developing a Model-View-Controller Component - Part 3 - Using the Database

Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface

That will help you get familiar with some of the functions available within Joomla's system and what they do (using the API). Take paticular care to understand the MVC (model view control) setup.

If however, you are absolutely new to PHP, following may help as a very short introduction to PHP.

PHP Introduction[edit]

PHP is a scripting language and is designed primarily for producing dyanmic web pages. It is in general embedded into a HTML page with tags like <?php ... ?>. When such a page is passed on to the PHP engine, the engine translates the PHP tags into HTML and which can then be rendered by a browser. Further during translation the script may require to pull data from a database and thus a database is needed. So for creating PHP pages we require in general, the PHP engine, a database typically MySQL and finally the webserver which will co-ordinate these actions and forward the final HTML to the browser. These 3 components can be installed separately, however, for convinience we have packages like WAMP for windows, MAMP for mac and LAMP for Linux. Installing the packages is discussed next.

PHP Installation[edit]

The packages can be downloaded from WAMP. WAMP stands for Windows Apache MySQL PHP. The instructions can be followed as given in the site and is pretty straightforward. The installation can be done is any directory (say D:/wamp). Once installed, it can be started from the program->start->WampServer. This should give a small icon in the system tray using which the server can be started/stopped/restarted. It also gives links to all important files like php.ini and other configuration files. For Mac users MAMP can be found here. For the purpose of this tutorial XAMPP can also be used.

PHP Round trip[edit]

Our primary target here is to write a small PHP page and get it rendered by the Apache web server. To render a page WAMP by default looks into the www folder of the WAMP install directory referred henceforth as WAMP_HOME for convenience. Fire up your primary text editor and create a page say first.php and save it in the WAMP_HOME/www/x/y folder, where x, y are folders (which you can create for organizing your scripts). Write the following in the script

<html>
<head>
   <title>Basic PHP Page</title>
</head>
<body>
<?php
echo ("My first php snippet");
?>
</body>
</html>

Save this file and point your browser to http://localhost/x/y/first.php and you should be able to see the echo message.

Primary things to note here are

1. It is strongly advised to start any PHP code by "<?php" and end with a "?>" tag. 
2. To have a semicolon after each PHP statement.
3. The directory in which this script resides needs to be given in the URL. 

This completes our round trip. Now for the sake of learning either you can keep including new elements in this file or may create separate scripts and load them in the browser.

PHP Variable declaration and Data types[edit]

For any programming we need to hold data. In PHP following are the ways in which you can define variables.

  1. $variable_name = 0;
  2. Note here we did not define what is the type of variable, whether integer or string that the variable should hold. This is referred to as dynamic scoping.
  3. the variable names should start with an alphabet and can contain alphanumeric characters and "_"
  4. Defining constants -
     define (“CONSTANT_NAME” , $value);

Basic operators[edit]

The general operators are as below and explained.

a.	+   e.g. 6 + 3, addition operator, answer is 5
b.	–   e.g. 6 - 3, subtraction operator, answer is 3
c.	/   e.g. 6 / 3, division operator, answer is 2
d.	*   e.g. 6 * 3, multiplication operator, answer is 18
e.	%   e.g. 6 % 3, modulus operator, answer is 0
f.	.   e.g. '6'.'3', concatenation operator, gives string '63'
g.	+=   e.g. $x += 6, adds 6
h.	-=   e.g. $x -= 6, subtracts 6
i.	/=   e.g. $x /= 6, divides by 6
j.	%=   e.g. $x %= 6, modulus by 6
k.	.=   e.g. $x .= '6', concats 6 to x
l.	<   e.g. $x < $y, evaluates to true if $x is < $y
m.	>   e.g. $x > $y, evaluates to true if $x is > $y
n.	==  e.g. $x == $y, evaluates to true if $x is equal by value to $y
o.	!=  e.g. $x != $y, evaluates to true if $x is not equal to $y
p.	===   e.g. $x === $y, evaluates to true if $x is and $y is identical i.e both of same type both value and reference
q.	>=  e.g. $x >= $y, evaluates to true if $x is >= $y
r.	<=  e.g. $x <= $y, evaluates to true if $x is <= $y
s.	&&  e.g. ($x == 2 && $y == 3) , evaluates to true if $x equals 2 and $y equals 3
t.	||  e.g. ($x == 2 || $y == 3) , evaluates to true if either $x equals 2 or $y equals 3
u.	xor e.g. ($x == 2 xor $y == 3) , evaluates to true if either $x equals 2 or $y equals 3 but not both
v.	++  e.g. $x++, increments $x
w.	–-  e.g. $x--, decrements $x

Defining array Structures[edit]

Ways of declaring arrays
Suppose we want many variables, we could have done in the way declaring variables as above, however, that is not elegant, for suppose u want a 100 variables; declaring arrays come to the rescue. Note that index in PHP starts from 0.

$my_guitar_heroes = array ("Hendrix", "Schenker", "Gilmour", "to_name_a_few!");

Or
$my_guitar_heroes [] = "Hendrix";
$my_guitar_heroes [] = "Schenker";
$my_guitar_heroes [] = "Gilmour";
Here PHP automatically assigns the index.

Or
$my_guitar_heroes [0] = "Hendrix";
$my_guitar_heroes [10000] = "Mainak";
Here in I just knew the first element and the 10000th element and thats me!

Or
a combination of the above means

Or
$my_guitar_heroes  = array_fill( 1, 9999, "to be decided" ); // set element 1 to 9999 as "to be decided"

'''Accessing the arrays''' 
print $my_guitar_heroes[2]; //Access the 2nd element

Or
$my_guitar_heroes[count($my_guitar_heroes)-1]; //Access the last but one element

Or
end($my_guitar_heroes); //The last element.

'''Iterating over the element of an array''' 
foreach ( $my_guitar_heroes as $temp) {
  print "$temp<br />";
}

Control expressions[edit]

The following are general syntax of the control structures


if statements

if( $x > 0) {	
 print("$x is positive");
}  else if( $x < 0 ) {	
 print("$x is negative");
} else {
 print("$x is 0");
}

for loop
for ( $x=1; $x<= 10; $x++ ) {

print "$x
";

}

while loop
while ( $x <= 10 ) {

print "$x
";

}

do while loop
do {

print "$x
"; $x++;

}while($x <= 10);

Defining functions[edit]

Debugging[edit]

Basic printing options

Handling forms[edit]

handling the Super global variables

Database actions[edit]