Introduction to PHP
PHP is a widely-used Open Source, general-purpose ,scripting language that is especially suited for Web development and can be embedded into HTML.
According to the php.net site, the acronym “PHP” is short for “PHP: Hypertext Preprocessor”. Its syntax draws upon C, Java, and Perl, and is
easy to learn.
The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP.
You can learn this language by understanding how its primary functions work, how it interacts with HTML, and how its nature of being a server-side scripting language differs from a client-sidelanguage, such as JavaScript.
PHP code must be executed on the server. Only the output it generates is delivered to client browser. Consider the following example.
A PHP script similar to the one above will not run on a client-side browser. It must be execute on a
Web server with the support of a PHP interpreter. The server, after execution, generates the string
output, as shown below, and sends it along with the rest of HTML tags to the client browser.
What the client receives is a completed HTML code looks like:
PHP supports all major operating systems, including Linux/ Unix, Microsoft Windows, Mac OS X,
and others. PHP has also support most of the web servers including Apache, Microsoft Internet
Information Server, Personal Web Server, Netscape, iPlanet servers, and many others.
Basically a web server, such as Apache, needs only to activate support for PHP and that all files
ending in .php are recognized as PHP code. To support a PHP-based data-driven site, you will most
likely want to create and use a database with database languages such as MySQL.
The php.net site
The “http://www.php.net” site is the primary web site for PHP language and support. This site
is where you can download the latest version of PHP along with manuals and other reference
documents. As of September 2010, the latest version is PHP 5.3.3. For Windows users, be sure to
download the Windows Binaries at “http://windows.php.net/download/”.
This site has a search engine. You can consider using it as a reference library. For example, to
search for date-related function, enter the keyword “date” in the textbox.
How does a PHP script look like?
PHP scripts are text files that can be created by any text editor, such as Notepad. It frequently
includes HTML tags. But instead of the .htm or .html extension, the files are given the .php
extension. Within the file, PHP code is contained in PHP tags.
Consider at a very simple PHP script that displays “Welcome to CIS246!” in the browser window.
The script is a text-only file, called example1.php, and the file is uploaded to the server.
This example is a simple but functional PHP script. It uses only an “echo” command to output a
string. Of course, PHP scripts used incommercial web sites will never be so simple. However, this
code gives you a chance to understand how PHP scripts work on the server side.
The PHP-enabled server reads the following HTML embedded PHP code and then executes the
“echo” command.
<?php
echo “Welcome to CIS246!”;
?>
The generated string “Welcome to CIS246!“ is then inserted between HTMLand
tags, as shown below, and then only the following HTML code is delivered to the client
browser.
<html>
<body>
Welcome to CIS246!
</body>
</html>
All of the PHP codes are processed and stripped from the page. The only thing returned to the client
from the Web server is pure HTML output. The source code of the above example tells us the
following:
• Because the page is rendered on the server before it hits the browser, the browser does not see
the PHP code.
• The entire <?php ?> tag is replaced by the HTML output from the PHP code.
• Every PHP statement ends with a semi-colon, “;”
Static vs.
dynamic
PHP code
PHP is an excellent tool for writing dynamic web pages. Non-technical users can easily learn a few
handy tricks to make their web pages easier to manage and more useful.
Technically speaking, all pages that contain PHP code will end in the extension .php. There are
basically two ways to create a PHP page. All PHP pages are variations on these two methods:
Method 1: Static with PHP Blurbs (HTML with embedded PHP code)
<HTML>
<BODY>
<?php
SOME PHP CODE HERE!
?>
</BODY>
</HTML>
Method 2: Dynamic by Default (PHP code with embedded HTML generated code)
<?php
PHP CODE
?>
There are some cases where you MUST use method 2, but for most of our work, you will be able to
use whichever method you prefer.
Beginning and Ending a Block of PHP Statements
When writing PHP, you need to inform the PHP engine that you want it to execute your PHP
commands. If you don’t do this, the code you write will be mistaken for HTML code and will be
output to the browser. You can do this with special tags such as, <?php … ?>, <? … ?>, or
<script>…</script> that mark the beginning and end of all PHP code blocks.
For example, the following three lines yield the same results.
PHP uses semicolon “;” as the statement delimiter.
Two basic output functions
The two basic PHP output functions are print and echo. These functions are used as follows:
Now, you might be wondering when to use each of these two functions if they’re are similar. Well,
there are really two differences.
Print will place a “newline character” (\n) at the end of each print statement so that you don’t
manually have to print out a new line. With echo, you really should use:
echo “Hello World\n”;
The \n is the representation of a newline. The PHP echo function can also accept multiple
arguments, as in:
echo “Hello “, “World!”, “\n”;
Commenting your PHP code
You can use either a single quote (‘) or a double quote (“) unless what you are trying to print
contains a variable or a special character (like \n). More details on that in later lecture.
Commenting your code is a good habit to practice. Entering comments in HTML documents helps
you (and others who might have to edit your document later) keep track of what’s going on in large
amounts of code. Comments also allow you to write notes to yourself during the development
process, or to comment out parts of code when you are testing your scripts, in order to keep code
from being executed.
HTML comments are ignored by the browser and are contained within <!– and –> tags. For
example, the following comment reminds you that the next bit of HTML code contains a logo
graphic:
<!– logo graphic goes here –>
PHP uses comments, too, which are ignored by the PHP parser (interpreter). PHP comments are
usually preceded by a double slash, like this:
// this is a comment in PHP code
However, you can also use the following type of comment syntax for multiple-line comments:
/* Put a multiple-line comment here */
# using Linux/Unix shell type of comment
Formatting PHP outputs using HTML tags FREE php course
Essentially, PHP is an inline scripting language developed for use with HTML. What this means is
that PHP and HTML can be intertwined with each other in a webpage. This means that not all of the
webpage must be generated by the PHP. You can use HTML formatting tags to format output
generated by PHP. For example,
The output will be:
You can also include the formatting HTML tags inside the double quotes of the PHP echo
command. For example,
Installing PHP FREE php course
The Windows PHP installer is built using Microsoft’s MSI technology, so its file name has an
extension of .msi (e.g.: php-5.3.0-nts-Win32-VC9-x86.msi). After downloading the PHP
installer, run the MSI installer and follow the instructions provided by the installation wizard. It will
install and configure PHP. There is a “Manual Installation Steps” guide that will help manually
install and configure PHP with a web server installed in a Microsoft Windows operating system. It
is available at http://www.php.net/manual/en/install.windows.manual.php.
For Mac OS X users, there are notes and hints specific to installing PHP on Mac OS X. Details are
available at http://www.php.net/manual/en/install.macosx.php.
For Linux users, simply refer to the manual provided by the distributer. For Fedora users, use the
following to install Apache (httpd), PHP, MySQL (server and client), and the component that allows
PHP to talk to MySQL using the root account.
yum -y install httpd php mysql mysql-server php-mysql
questions FREE php course
B. Personal Home Page
C. PHP: Hypertext Preprocessor
D. Personal Hypertext Processor
B. <?php…?>
C. <script>…</script>
D. <&>…</&>
B. Document.Write(“Hello World”);
C. “Hello World”;
D. Console.Write(“Hello world”);
A. $
B. !
C. &
D. #
B. ;
C. New line
D. </php>
B. /*…*/
C. <!–…–>
D. <comment>…</comment>
B. http://www.php.gov
C. http://www.php.net
D. http://www.php.org
B. <?php <i>”Welcome to CIS246!”</i>; ?>
C. <?php echo_i(“Welcome to CIS246!”); ?>
D. <i><?php echo “Welcome to CIS246!”; ?></i>
B. <html></body>echo “CIS246!”; </body></html>
C. <html></body>”CIS246!”</body></html>
D. <html></body>CIS246!</body></html>
B. php-5.3.0-nts-Win32-VC9-x86.com
C. php-5.3.0-nts-Win32-VC9-x86.php
D. php-5.3.0-nts-Win32-VC9-x86.msi