PHP is a server-side scripting language

Arya MirSoftware and s/w Development

May 15, 2012 (5 years and 5 months ago)

760 views

PHP stands for PHP: Hypertext Preprocessor • PHP is a server-side scripting language, like ASP • PHP scripts are executed on the server • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) • PHP is an open source software • PHP is free to download and use

Week 2: Intro to PHP

Ajam – DMIS 23b
1


PHP is a server-side scripting language.

What is PHP?

PHP stands for PHP: Hypertext Preprocessor

PHP is a server-side scripting language, like ASP

PHP scripts are executed on the server

PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, etc.)

PHP is an open source software

PHP is free to download and use
What is a PHP File?

PHP files can contain text, HTML tags and scripts

PHP files are returned to the browser as plain HTML
What is MySQL?

MySQL is a database server

MySQL is ideal for both small and large applications

MySQL supports standard SQL

MySQL compiles on a number of platforms

MySQL is free to download and use
PHP + MySQL

PHP combined with MySQL are cross-platform (you can develop in
Windows and serve on a Unix platform)
Why PHP?

PHP runs on different platforms (Windows, Linux, Unix, etc.)

PHP is compatible with almost all servers used today (Apache, IIS, etc.)

PHP is FREE to download from the official PHP resource:
www.php.net


PHP is easy to learn and runs efficiently on the server side



Week 2: Intro to PHP

Ajam – DMIS 23b
2


Basic PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the
browser.
A PHP scripting block always starts with <?php and ends with ?>. A PHP
scripting block can be placed anywhere in the document.
A PHP file normally contains HTML tags, just like an HTML file, and some PHP
scripting code.
Below, is an example of a simple PHP script which sends the text "Hello World"
to the browser:
<html>

<body>

<?php
echo "Hello World";
?>

</body>
</html>


Each code line in PHP must end with a semicolon. The semicolon is a separator
and is used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print.
Note: The file must have a .php extension. If the file has a .html extension, the
PHP code will not be executed.
Exercise 1:
Echo the following and record the outcome:
1. echo “Hello”;
2. echo 100
3. echo “Hello” , “World”;
4. echo Hello World;
5. echo “Hello” , “ World”;
6. echo “Hello World”;
Week 2: Intro to PHP

Ajam – DMIS 23b
3


Variables in PHP
Variables are used for storing values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;

New PHP programmers often forget the $ sign at the beginning of the variable. In
that case it will not work.
Let's try creating a variable containing a string, and a variable containing a
number:
PHP Code:
<?php

$txt="Hello World!";
$x=16;
?>


Display1:


Display2:




Week 2: Intro to PHP

Ajam – DMIS 23b
4

PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type
the variable is.
PHP automatically converts the variable to the correct data type, depending on
its value.
In a strongly typed programming language, you have to declare (define) the type
and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.
Naming Rules for Variables

A variable name must start with a letter or an underscore "_"

A variable name can only contain alpha-numeric characters and
underscores (a-z, A-Z, 0-9, and _ )

A variable name should not contain spaces. If a variable name is more
than one word, it should be separated with an underscore ($my_string), or
with capitalization ($myString)
Outputting a String Variable
To output a string, use PHP echo as well. You can place either a string variable
or you can use quotes, like below, to create a string that the echo function will
output.
PHP Code:
<?php
$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";
?>
Display:
Hello!
I love using PHP!
Week 2: Intro to PHP

Ajam – DMIS 23b
5

In the above example we output "Hello!" without a hitch. The text we are
outputting is being sent to the user in the form of a web page, so it is important
that we use proper HTML syntax!
In our second echo statement we use echo to write a valid Header 5 HTML
statement. To do this we simply put the <h5> at the beginning of the string and
closed it at the end of the string. Just because you're using PHP to make web
pages does not mean you can forget about HTML syntax!
Careful When Echoing Quotes!
It is pretty cool that you can output HTML with PHP. However, you must be
careful when using HTML code or any other string that includes quotes!
Echo uses quotes to define the beginning and end of the string, so you must use
one of the following tactics if your string contains quotations:

Don't use quotes inside your string

Escape your quotes that are within the string with a backslash. To escape
a quote just place a backslash directly before the quotation mark, i.e. \"

Use single quotes (apostrophes) for quotes inside your string.
See an example below for the right and wrong use of echo:

PHP Code:
<?php
// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">I love using PHP!</h5>";

// OK because we escaped the quotes!
echo "<h5 class=\"specialH5\">I love using PHP!</h5>";

// OK because we used an apostrophe '
echo "<h5 class='specialH5'>I love using PHP!</h5>";
?>
If you want to output a string that includes quotations, either use an apostrophe (
' ) or escape the quotations by placing a backslash in front of it ( \" ). The
backslash will tell PHP that you want the quotation to be used within the string
and NOT to be used to end echo's string.
Week 2: Intro to PHP

Ajam – DMIS 23b
6


Echoing Variables
Echoing variables is very easy. The PHP developers put in some extra work to
make the common task of echoing all variables nearly foolproof! No quotations
are required, even if the variable does not hold a string. Below is the correct
format for echoing a variable.
PHP Code:
<?php
$my_string = "Hello Bob. My name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>
Display:
Hello Bob. My name is: 4a
Echoing Variables and Text Strings
You can also place variables inside of double-quoted strings (e.g. "string here
and a $variable"). By putting a variable inside the quotes (" ") you are telling PHP
that you want it to grab the string value of that variable and use it in the string.
The example below shows an example of this cool feature.
PHP Code:
<?php
$my_string = "Hello Bob. My name is: ";
echo "$my_string Tony <br />";
echo "Hi, I'm Bob. Who are you? $my_string <br />";
echo "Hi, I'm Bob. Who are you? $my_string Tony";
?>

Week 2: Intro to PHP

Ajam – DMIS 23b
7


Display:
Hello Bob. My name is: Tony
Hi, I'm Bob. Who are you? Hello Bob. My name is:
Hi, I'm Bob. Who are you? Hello Bob. My name is: Tony
By placing variables inside a string you can save yourself some time and make
your code easier to read, though it does take some getting used to. Remember to
use double-quotes, single-quotes will not grab the value of the string. Single-
quotes will just output the variable name to the string, like )$my_string), rather
than (Hello Bob. My name is: ).

Exercise 2:
1. Create a PHP page
2. Insert on the page a few HTML lines including H1, H2, a <p>, a bold, and
a <br />.
3. Create a minimum of 5 variables
4. Create an echo output statement to process the 5 or more variables/Text
Strings, while having the output be part of your html headers and
paragraphs.
5. Save your work on the server under week 2, with an index page in your
root folder listing with links week 1 and week 2.