PHP is a server-side scripting language. Before starting to learn PHP,
one should already have basic understanding of HTML or XHTML and of JavaScript.
PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting
language, similar to ASP. This means that scripts are run on a server and the
client gets the result of the script execution. PHP supports a large number of
databases as well, such as MySQL, Oracle, and more. The biggest advantage of
PHP is that it is an open source project and it is free to download and use.
To start developing with PHP, one needs access to a web server with
PHP support. The two most popular are Apache and IIS. Several options exist on
how to implement your server setup. For testing, many people prefer to use
XAMPP, which I mentioned in another blog. XAMPP comes ready with all the
features required installed. However, it is not recommended to use XAMPP on
production systems. Also, for most of the functions, MySQL is also required.
This is also available for free.
With PHP up and running, it is fairly simple to get started. PHP code
is written between the opening tag and the closing tag.
<?php
echo "Hello World";
?>
Also, for the code to execute on the server, the file extension must
be .php, even though this file might include HTML and JavaScript. Most of the
times, we want the script to output something to the screen. This might be in
the form of informative messages, such as "Hello World", and it can
also be to generate HTML code to populate a table with results from a database,
such as the code snippet below:
<?php
while (($row = mysql_fetch_array($sql))) {
print("<tr><td>".(ucwords($row[0]))."</td><td>".$row[1]."</td></tr>");
}
?>
The code above inserts the row tags, <tr>, and the column tags, <td>,
together with the information to be inserted in the table. As you might noted,
there are two methods for outputting messages: echo and print.
Variables in PHP
Variables are used in PHP similarly to other programming languages. Variables
start by the $ sign in front of the name. Variable named cannot start with a
number or special character and they are case sensitive. Variables in PHP can
have different variable scopes: local,
global, static, and parameter.
Local Scope variables are declared within a function and can only be
accessed within that function. Global Scope variables are any variables
declared outside a function and these can be accessed from anywhere. To access
them from functions, the keyword global must be used. Static Scope variables
are variables which are not deleted and these are declared by using the keyword
static. Parameters are local variables whose value is passed to the function by
the calling code.
String Operations and Functions
In PHP there is only one string operator. The is the concatenation
operator (.). This is used to put two string values together forming one
string. The code below shows two strings being concatenated:
<?php
$txt1="Hi, my name is ";
$txt2="Daniel Borg!";
echo $txt1 . " " . $txt2;
?>
$txt1="Hi, my name is ";
$txt2="Daniel Borg!";
echo $txt1 . " " . $txt2;
?>
The code above will output "Hi, my name is Daniel Borg!".
Several functions related to strings exist to facilitate the life of a PHP
developer. These include strlen() which returns the length of the string and
the strpos() which returns the position of a character within the string
specified.
PHP, like all other languages, has its own set of arithmetic,
assignment, incrementing, and decrementing operators. It also has some array
operators which are shown below:
Operator
|
Name
|
Description
|
x
+ y
|
Union
|
Union
of x and y
|
x
== y
|
Equality
|
True
if x and y have the same key/value pairs
|
x
=== y
|
Identity
|
True
if x and y have the same key/value pairs in the same order and of the same
types
|
x
!= y
|
Inequality
|
True
if x is not equal to y
|
x
<> y
|
Inequality
|
True
if x is not equal to y
|
x
!== y
|
Non-identity
|
True
if x is not identical to y
|
Forms
These are variables which are used to retrieve information from forms.
THe code below shows a very simple form:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
When a user fills out this form and submits it, the data is sent to
the PHP file welcome.php. The welcome.php code is show below:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
The output
of the page would be:
Welcome Daniel!
You are 24 years old.
Ideally, the forms will have some validation mechanism to remove
security threats and to control the input of data.
$_GET Variable
The $_GET variable is used to collect values in a form with
method="get". The information passed when the GET method is used is
shown in the address bar, making it unsafe to use for login screens. It is also
limited in the amount of information which can be sent. The code below shows a
form using the GET method:
<form
action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the submit button is clicked, the URL sent to the server will
look like this:
yourdomain.com/welcome.php?fname=Daniel&age=24
The code in
the welcome.php can make use of the values passed by using the $_GET variables
as shown below:
Welcome
<?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
You are <?php echo $_GET["age"]; ?> years old!
Which will return:
Welcome Daniel.
You are 24 years
old!
The get method should not be used when
information is sensitive and for very large variable values.
$_POST
Variable
The $_POST variable is used to collect
values from forms sent with method="post". Information sent using the
post method is invisible to others. The maximum size for the POST method is 8MB
but this can be changed in the php.ini file (post_max_size). The syntax is
identical to the $_GET. The disadvantage of the post method is that the page
cannot be bookmarked.
$_REQUEST
This variable contains the values of
the $_GET, $_POST, and $_COOKIE. It can be used to collect form data sent with
both GET and POST methods.
No comments:
Post a Comment