PHP has been around for a few years now and has developed as a language. Why should you use it over ASP, JSP, or Perl when you are building a web application? With a veritable alphabet of programming languages to choose from when you decide to write a web based application, why should you choose PHP over ASP, JSP, Perl, CGI, or Cold synthesis? While any decision you make also has to be based on the business supplies of the application, as a consultant you often have the choice of what language to use. So barring an IT department that tells you “It’s Microsoft or nothing!” or a gaggle of UNIX developers demanding that all their applications be Java based, here are a few reasons why PHP is the best solution for web applications. It was created for the web – PHP started as a collection of C applications that the author wrote for use on his own web site. It evolved into a server-side scripting language built for building logic into web pages. This firmness in design has resulted in a language that is both flexible and streamlined for use on the web. Its cross-platform – PHP runs on UNIX, Linux, Mac OSX, and Windows. In addition to being cross-platform, it has built in functions for connecting to most popular database, including general ODBC connections. It’s cross-language – What does that mean? It’s a term I coined to show that PHP can use objects in languages other than PHP. It can call Windows COM objects and Java objects, making it easier to separate the business logic from the user interface. The PHP team is even working on letting it call .NET meeting. The syntax is based on C – Being based on C makes it easier for Java, C, even JavaScript programmers to pick up. It also has built in functions for handling regular expressions, XML parsing, and cryptography. You can compile in different libraries that allow you to manipulate graphics. One feature of the language where it breaks from the C syntax is it’s object-oriented nature. It supports the creation of custom classes and allowing other classes to inherit from your custom classes. Future releases should further enhance it’s object-oriented capabilities. PHP is much faster than CGI – It’s not as fast as JSP’s, which are compiled, but it is faster and uses less system resources than CGI programs do. My final reason for choosing PHP over other languages has to do with the popularity of the languages. Thousands of web pages are running PHP and it has a dedicated following. If you choose to learn PHP, you can be assured that you can find enough resources to answer any question you have and in most cases you’ll find that someone has already written code that does exactly what you need and is giving it away for free.
Each of these technologies have their strengths and weakness but overall for beginners getting into the game of dynamic websites There are some reason to choose this language
1. It is so popular that cheap PHP hosting is not hard to find.
2. It is free.
3 It is easy to learn
4. It is flexible and powerful.
5. PHP is probably the most popular web scripting language and environment out today so there is a lot of freely available information out there on it.
In general when a new software version appears everybody hurries to update it particularly if it is for free. PHP 5 has just come into view and it seems that there are people who use the PHP 4 version and others who use the PHP 5 one.
You most likely know that PHP 5 is destined for OOP, but it appears that usual programming can be used too. Furthermore, OOP is used in PHP4 as well, with the difference that in PHP5 things are a little more evaluated. This means that in PHP4 safety modes for classes (public, private) are not accepted. In PHP4 the objects are a kind of structures which accept objects and functions as well, according to OOP. In PHP4 they are useful as well.
If you are used to working with PHP4 you can use PHP5 with no problems because the differences are not important and the changes were made so that programmers would not be confused. An example would be class builders which, in PHP4 were functions within the classes bearing the same name as the class. In PHP5 it is firstly checked if there is a function (method) __construct (). If it does not exist, check if there is a function (method) which has the same name as the class. This means that even if you are not aware of the latest news in the domain of PHP5, your scripts will function without any problem.
If you want to install PHP4 and PHP5 in parallel, you can set USE indicators, which are different for each version and you can find out a lot of other important pieces of information asking us.
During work with any language we make use of variables. Variables are used to store values and reuse them in our code. We use different types of variables in our code such as strings (text), integers (numbers), floats (decimal numbers), Boolean (true or false) and objects. In PHP we can make use of variable while writing scripts. In this lesson we’re going to cover PHP variables.
PHP Variable Syntax
$var_name = value;
Important Variable in PHP?
Some key points to notice:
(1) Remember to put the $ sign in front of variables when declaring variables in PHP.
(2)Variable names must start with letters or underscore.
(3) Variables can’t include characters except letters, numbers or underscore.
What is a variable?
A variable is a indicate to store values such as strings, integers or decimals so we can easily reuse those values in our code. For example, we can store a string value such as “Hello world” or an integer value of 100 into a variable.
PHP variable types?
Different Java or C++, PHP doesn’t care about primitive types. Any variable, a string, an integer or a float is declared the same way. PHP converts the types in the code by itself. Here’s what I mean.
//an integer variable $var_name = 300;
//an float variable $var_name = 300.00 ?>
PHP Variable type manage
Like talk about above, PHP doesn’t require variables to declared using primitive types. Therefore, manage between two types doesn’t require use of any special function. We can simply do things like…
//string var $var = “0″;
//var is now float $var += 8.5;
//var is now integer $var += 3;
//var is now string $var .= ” is the sum”; echo $var;
?>
Concatenating variables in PHP?
In PHP we can join two variables by using the dot ‘.’ operator.
$var1 = “Hello world”; $var2 = “and Java”;
//prints “Hello world and Java” echo $var1 . $var2;
$var1 = “1″; $var2 = “2″;
//prints “12″; echo $var1 . $var2;
?>
So there you have it, a quick and easy variable lesson in PHP.
Javascript can be used with PHP without problems, as long as you keep in mind that they operate in different environments (see Frames, JavaScript, and PHP Overview):
PHP, is server side. Therefore, if you want to pass data from a form to PHP, you have to submit it and load the page again. There is no getting about it.
Javascript is client side (run in the browser), and can help you with dynamic functionality. You can easily use JavaScript and PHP jointly. However, just like PHP knows nothing about Javascript. PHP will just echo the Javascript code to the HTML page, just as it would echo the value of a string variable.
EXAMPLE
<?php
echo ‘Welcome to PHP’;
?>
and
<?php
echo ‘<script type=”text/javascript”>
<!–
if ( 1 ) {
window.open(\’any.php?name=News\’, \’hello world\’,
\’HEIGHT=500, resizable=yes, WIDTH=500\’);
}
//–>
</script>’;
?>
They both echo strings, of which the first one is simply displayed in the client’s browser as a welcome message, while the second one is take by the client’s browser and leads to the browser sending a request for the News module, which in turn sends the usual home page of a PHP site to be displayed on the client.
With this in brain, Javascript does not demand any special treatment when used in PHP blocks. Just append the Javascript code to the $content variable of the block and you are done. Of course, you have to escape double quotes – and it is a good idea to add a newline (\n) to the end of each Javascript line (see Javascript and BLOCKs).
If you use sed, the following sequence of set commands, applied to a Javascript file, will produce the right PHP code to include in the PHP block:
(A) 1,$s/”/\\”/g
(B) 1,$s/^/\$content \.= “/
(C) 1,$s/$/”;/
(D) 1,$s/”;$/\\n”;/
A Escape all double quotes.
B. Add a newline \n at the end of each JS line
C Add the string ‘”;’ at the end of every line.
D. Add the string ‘$content .= “‘ at the start of every line
Server Side Includes
You can insert the content of a file into a PHP file before the server carry out it, with the include() or require() function. The two functions are identical in every way, except how they handle errors.
The include() function generates a warning (but the script will continue execution)
The require() function generates a fatal error (and the script execution will stop after the error).
This can save the developer a substantial amount of time. This means that you can develop a standard header or menu file that you want all your web pages to include. When the header needs to be updated, you can only update this one include file, or when you add a new page to your site, you can simply change the menu file
The include() Function
The include() function get all the text in a particular file and copies it into the file that uses the include function.
Example
suppose that you have a standard header file, called “welcome.php”. To include the header file in a page, use the include() function, like this:
<html>
<body>
<?php include(“welcome.php”); ?>
<h1>Welcome</h1>
<p>any text</p>
</body>
</html>
Example
Now, let’s suppose we have a standard menu file that should be used on all pages (include files usually have a “.php” extension). Look at the “any.php” file below:
<html>
<body>
<a href=”http://www.any.com/xxx.php”>hi</a> |
<a href=”http://www.any.com/zzz.php”>your Us</a> |
<a href=”http://www.any.com/yyy.php”>name Us</a>
The three files, “xxx.php”, “zzz.php”, and “yyy.php” should all include the “any.php” file. Here is the code in “xxx.php”:
<?php include(“any.php”); ?>
<h1>Welcome</h1>
<p>any text</p>
</body>
</html>
If you look at the source code of the “xxx.php” in a browser, it will look something like this:
<html>
<body>
<a href=”xxx.php”>hi</a> |
<a href=”zzz.php”>your Us</a> |
<a href=”yyy.php”>name</a>
<h1>Welcome</h1>
<p>any text</p>
</body>
</html>
And, of course, we would have to do the same thing for “zzz.php” and “yyy.php”. By using include files, you simply have to update the text in the “any.php” file if you decide to rename or change the order of the links or add another web page to the site.
The require() Function
The require() function is indistinguishable to include(), except for that it handles errors differently.
The include() function produce a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).
If you include a file with the include() function and an error occurs, you might get an error message like the one below.
PHP code
<html>
<body>
<?php
include(“phpguru.php.php”);
echo “Welcome!”;
?>
</body>
</html>
Error message:
Warning: include(phpguru.php) [function.include]:
failed to open stream:
No such file or directory in on line 5
Warning: include() [function.include]:
Failed opening ‘phpguru.php’ for inclusion
(include_path=’.;C:\php\ar’)
on line 5
Welcome!
Now, let’s run the same example with the require() function.
PHP code:
<html>
<body>
<?php
require(“phpguru.php “);
echo “Welcome!”;
?>
</body>
</html>
It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.