Sep
29
2008

PHP Basics: Syntax

This article covers basic PHP syntax, including PHP Tags, variable usage, variable types, and several ways of printing variables to the web browser.
I remind you that you require some knowledge of PHP, as this blog is a revision aid not intended to educate from scrach. 

Define: Syntax

The syntax of a programming language describes the structure of programs. The rules for the construction of a command or statement. 

PHP’s Syntax is derived mainly from the C and Perl languages. OOP in PHP is majoritly derived from Java, the choice of syntax from each was chosen to aid simplicity.

PHP Tags

PHP has four opening and closing tags but two of them (Short Tags & ASP Tags) are no longer used and shouldent feature in the test.
Standard Tags:

<?php echo "Hello World"; ?>

Short Tags

<? echo "Hello world"; ?>

ASP Tags

<% echo "Hello World"; %>

As you can clearly see theirs a tag at the start of the PHP and their is a tag at the end, I prefer the Short Tags but if used it may cause conflict with XML Headers! If you have to display a variable in middle of some html you can also use this:

<?=$variable;?>

Terminate All Instructions

Get ready for the character; to be worn out! This must be used at end of everything! Every function, variable assignments, data outputs … Everything:

example_function();
$exam_variable = "Hello World";
echo $exam_variable;

As you can see, at end of every statement we need a semicolon ;

Comments

Comments is what you write to remind you later what the hell the code means! Lots of comments are needed but not required in your code. Comments do not effect the output or how your code is used!
Their are many ways to show comments in PHP!
Single Line Comment:

// Comments can be funny

Single Line Comment 2:

# but should be descriptive

Multi-Line Comment:

/* Comments do nothing
effecting the output of your code
*/

Multi-Line Comment 2:

/**
* Comments are useful
* for documenting your code for future reference
* or to help others to understand.
*/

Newline Characters

Don’t go putting newlines (or Whitespace) before a PHP Document as it causes lots of problems, especially if you use the Headers in PHP (such as redirects, gd images, using php as file processor).

Whitespace Characters

Whitespace also known as ” ” or a space. Ensure you do not put whitespace characters before PHP Output. You’re unable to put spaces in PHP Tags, nor break apart keywords or functions or variable names. All These Are Invalid:

< ?php
func tion
$var iable

Code Blocks

Code Blocks are used a lot in PHP such as in conditional statements, functions and classes. In basic terms its a section of code that can only run if it meets your requirements.
Code Block:

{
//Some PHP Code
}

Code Block in Conditional Statement:

if(1 == $sumnumber){
//Some PHP Code
}

Code Block in Function:

 function addnumbers($num1, $num2){
//Some PHP Code
}

Code Block’s are a large part of programming, they are very useful for dynamic content.

Language Constructs

Constructs are built-into PHP one of which is echo the most used peace of code in php? … echo is used to output data to the scripts output. You can write all day but without echo (or print) the user wont be able to see anything but a blank page!

echo "Hello World";

The code above will show the user the text Hello World, that’s all great but Echo does not have a return value (a conditional statement, can not tell if its been executed or not!) therefore we have print this acts just like echo except it returns the value 1 if requested.

Data types

As PHP is used for everything! Their are many data types ranging from numeric, alphabetic and other. Im not going to get too involved in these, but good knowledge is nessasary!
boolean - value can be true or false
int - a numberic value
float - a floating point value
string - a collection of binary data (anything!)
Numeric:
decimal - for decimal numbers
octal - unix-style access permissions
hexadecimal - base-16 hexadecimal digits

You can output data with data types in mind, like such:

 echo (int) (51 + 5.1);
//or
$x = 5.55
echo (int) $x; //outputs 5!

This method is also used in converting data to another datatype. Their are some problems with datatypes and they do become confusing. Their are limitations and you should know them for coding.

Data Type Strings by bad coders are thought of as text and numbers. Since you are reading this blog you should be aware that Strings in PHP are collections of binary data (this could be text!). They can be used for many other things, in the past I have used strings to store images! …. But I May blog about strings later.

An other datatype that is used in PHP is NULL this means that it has been assigned nothing! It is used in many aspects such as defining variable contents, passing data to functions and even when submitting data via SQL…. In lame terms NULL = Nothing .

Compound Data Types
This is a strange name for saying Arrays and Objects. Arrays is a nice and clean way of asigning data ordered or not. Arrays are great and can contain everything, even other arrays ! More details about Arrays Later.Compound Data Type is also the umbrella term for Objects in Object Orientated Programming. OOP is not my speciality, so we can talk about this later after I have read more!

PHP Basics: Syntax End?

I have covered what I know, I have covered aspects in the Book. I have not covered what is in the test, as currently I do not know!

If you think I have missed something, don’t by Shy to comment as It can be added! Pluss id like it, as I have not taken the test yet! Any mistakes follow the same curtsy!

Thanks for reading!
Adam
http://zend.is-hacked.com

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS