Welcome to PHP

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Post Reply
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

Welcome to PHP

Post by Scott »

PHP has quickly become one of the most popular web-development languages in the world ([url=\"http://www.php.net/usage.php\"]usage statistics[/url]), so why wouldn\'t you want to learn it? Easier said than done, but if you do want to learn at least a little bit of PHP then listen up. This how-to will run you through the fundamentals of PHP. However, before then lets take a look at the facts:[list]
[*]PHP stands for [b]PHP: Hypertext Preprocessor[/b] (its a recursive acronym)
[*]PHP is a scripting language which is interpreted instead of compiled
[*]PHP is developed using syntax from C, Perl and Java including some new creations aswell.
[*]PHP is currently on its 5th major release, however PHP 4 is still the most used version.
[*]More information or installation help can be found at [url=\"http://www.php.net\"]PHP.net[/url]
[/list][b][u]PHP Tags[/u][/b]
In order for the PHP interpreter to start parsing PHP script, you must first define a start and stop point for PHP instructions. A number of tags can be used, depending on their avaliability. The tags supported by PHP are:[list]
[*]The normal php tags [b]<?php[/b] to open and [b]?>[/b] to close
[*]Short PHP tags [b]<?[/b] and [b]?>[/b]
[*]HTML type script tags [b]<script language=\"php\">[/b] and [b]</script>[/b] (seldom used)
[*]ASP type tags [b]<%[/b] and [b]%>[/b]
[/list][u]Note:[/u] The short and ASP tags may not be avaliable depending on the configuration of PHP. The short tags also can cause problems with XML tags and therefore programmers are encouraged to always use the full [b]<?php[/b] tag to increase portability.

[b][u]Output tags[/u][/b]
PHP has special [b]<?=[/b] and [b]?>[/b] tags which can be used to output an expression straight to the browser. Again this requires prior configuration to work.

[b][u]echo and print[/u][/b]
PHP has two constructs which output expressions to a browser. Neither of these are functions and therefore parenthesis isn\'t required: [code]<?php
print("Hello");
//works the same as
print "Hello";
?>[/code]
There is a subtle difference between the two constructs - print will act as a function and will always return a Boolean value. Since it is not required to return any value, echo is usually faster, although only marginally so.

[b][u]Satements[/u][/b]
A statement is a command which the interpreter must execute. This could be an expression, call to an object or function or numerous other constructs built into PHP. A simple example statement is: [code]echo "This is a statement";[/code] A statement must end in a semicolon to avoid a parse error from the interpreter.

[b][u]Variables[/u][/b]
To make PHP similar to almost every other language PHP facilitates the use of variables. Variables can store one value at a time (including an array) and can be changed numerous times in the execution of a script (they can be varied...hence the name). In PHP variables are prefixed with the dollar sign ($) and [i]are[/i] case sensitive, so $variable is different from $Variable. Also, unlike certain languages (eg VB) PHP doesn\'t require the variable to be declared prior to use.

[b][u]Constants[/u][/b]
The opposite of a variable is a constant. Once defined it cannot be changed during the execution of a script. A constant must be created using the [b]define()[/b] function, here is an example: [code]<?php
define("CONSTANT", 17);
echo CONSTANT;
?>[/code]The [b]define()[/b] function takes 2 arguments (and 1 optional). The first is a string of the name of our constant, in this instance \"CONSTANT\". The second is the value of our constant, here we set it to the integer 17. The third optional arugment specifies whether the constant is case [b]in[/b]sesitive. By default a constant is case sensitive (uppercase by convention - variables lowercase) but passing [i]true[/i] as the last argument will cause (for example) CONSTANT to be the same as CONstanT.


Hopefully that shows you the basics of the PHP language. I\'ve tried to make this as clear as possible, but if something is unclear - just ask <img src=\'http://www.killanet.net/forum3/public/s ... >/good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' />
Post Reply

Return to “PHP &amp; MySQL”