0% found this document useful (0 votes)
458 views16 pages

Chapter 1 - Essential PHP Spring Into PHP 5 by Steven Holzner

This document provides a summary of 3 key points about PHP: 1. PHP is a server-side scripting language that is embedded into HTML documents and is used for form handling, file processing, and database access. 2. PHP code is interpreted by the server and the output is returned to the client. Variables in PHP always begin with '$' and can be of different data types like integers, floats, strings, arrays, and objects. 3. PHP supports different types of comments for code readability and uses functions like echo and print to output text. Constants in PHP are defined using define and cannot change value once set.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
458 views16 pages

Chapter 1 - Essential PHP Spring Into PHP 5 by Steven Holzner

This document provides a summary of 3 key points about PHP: 1. PHP is a server-side scripting language that is embedded into HTML documents and is used for form handling, file processing, and database access. 2. PHP code is interpreted by the server and the output is returned to the client. Variables in PHP always begin with '$' and can be of different data types like integers, floats, strings, arrays, and objects. 3. PHP supports different types of comments for code readability and uses functions like echo and print to output text. Constants in PHP are defined using define and cannot change value once set.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 1 - Essential PHP

spring into PHP 5


by Steven Holzner

Slides were developed by


Jack Davis
College of Information Science
and Technology
Radford University

August 2006 1
Origins and Uses of PHP
- Origins
- Rasmus Lerdorf - 1994
- Developed to allow him to track visitors to his
Web site

- PHP is an open-source product

- PHP is an acronym for Personal Home Page, or


PHP: Hypertext Preprocessor

- PHP is used for form handling, file processing,


and database access

Overview of PHP
- PHP is a server-side scripting language whose
scripts are embedded in HTML documents

- Similar to JavaScript, but on the server side

- PHP is an alternative to CGI, ASP.NET, JSP, and


Allaire’s ColdFusion

- The PHP processor has two modes:


copy (XHTML) and interpret (PHP)
August 2006 2
Review of the HTTP
request response process

- Client issues the request for a given URL (the


browser)
- Server receives the request and returns the
document (html) to the client
- Browser receives the document from the
server and renders the document

- Request could be for an html document or


could be for some other file
- For server side programs the requests are
issued for other file types, like PHP

August 2006 3
Radford University
PHP file processing

- Check to see if there is a dynamic_php


directory on your RU virtual drive.
It will be under your home directory.
So, for me – the path is
//homedir/users/jcdavis/dynamic_php

- If the directory (folder) isn’t there you need


to create it.
- If permissions are appropriately set, then
you can put php files in this directory and
they will be served, by the campus web
server.
- The URL will be:
- https://php.radford.edu/~user/file.php

- Example:
https://php.radford.edu/~jcdavis/p1.php

August 2006 4
PHP Processing

• You can mix PHP and xhtml in a single file


• When the client sends a request for a PHP
file, the web serving software interprets the
file

It executes the PHP and copies the xhtml into


an output file to be returned to the client
• So, a PHP file could just contain valid xhtml

file ---- firstphp.php

<html>
<head><title>First PHP
</title></head>
<body>
<h1>
<First PHP>
</h1>
</body>
</html>
• The above is a valid PHP program.
August 2006 5
Adding Code

• When the previous file was requested. The


web serving program will get the request, see
that it is a php file. It will examine the file, one
line at a time. It copies html statements into
the output file. When it encounters php code,
it translates them and puts the output (if any)
into the output file. The created output file is
returned to the client. It must be a valid html
file.

• PHP file with code

<html>
<head><title>PHP + CODE</title></head>
<body>
<h2>PHP with Code</h2>
<?php
phpinfo(); # valid php function
?>
</body>
</html>
• What’s wrong with the above????
August 2006 6
Printing Text

• echo
The echo statement inserts text into the
output file.

• <html>
<head><title>ECHO</title></head>
<body>
<h1>Echo text.</h1>
<?php
echo "<p>";
echo "Hello from PHP.";
echo "</p>";
?>
</p>
</body>
</html>

• What’s wrong with the above?


• https://php.radford.edu/~jcdavis
/it325examples/phpecho.php

August 2006 7
PHP Outputs HTML

• The output of a php program that is intended


for the use of a browser, should contain all the
appropriate html tags. In addition, usually
we’ll want the source code to be structured
well, so you’ll need to include the appropriate
carriage controls.

<html>
<head><title>Carriage Controls</title></head>
<body>
<?php
echo "<p>Here’s a paragraph output via
echo note we can have text on
multiple lines without ending the
quote.</p>";
echo "<p>A secondparagraph. </p>";
echo "\nTo structure the html include new-
linecharacters.\n"
?>
</body>
</html>

August 2006 8
Formatting Characters

• \n newline
• \r carriage return
• \t tab
• \\ displays a \
• \$ displays a $ (\ is escape char.)
• \” displays “

• echo "he said, \"I like ice cream.\"";

• You can also use the print statement, which is


a php function, so it returns a value.
Normally, this is of little use, so it’s use is
almost identical to echo.

• print ("he said, \"I like ITEC 325\".");

August 2006 9
PHP Comments

• Three ways to indicate a comment in php.

/* Multiline comments
use asterisk and slash */

// Single line comment

# Single line comment

• <?php
/* here’s a comment,
with more than one line */

print ("PHP is fun!"); # print statement


?>

• Remember use html comments in html


sections, and php comments inside php code
• <html>
<!-- echo.php -->

August 2006 10
PHP Variables

• We prefix all variable names with a $ in PHP.

Valid variable names start with a letter or


underscore, followed by any number of
letters, numbers, or underscores, and the
name can be any length. Normally, PHP
programmers use lower case letters for
variable names.

$pizza $counter $_number

$planet_number_9

• Assignment statements are typical.

$temp = 90;
$num_of_students = 23;
$pi = 3.1415;
$class_name = "ITEC 325";

echo "The class name is ", $class_name;

August 2006 11
Interpolating Variables

• Inside double quotes, variables will be


replaced by their values.

<?php
$dept = "ITEC";
echo "The department is $dept.";

• Sometimes you need to separate the variable


name from a literal string that you want ouput
next to it.

$text = "news";
echo "Where’s the {$text}paper?";

August 2006 12
Reference Variables

• You can create reference variables in PHP.

$apples = 4;

$fruitname = "apples";

Now you can refer to the value in $apples as $


$fruitname.

echo "number of apples: ${$fruitname}";

$apples = 4; $oranges = 3;
$fruitname = "oranges";
echo "number of {$fruitname}: ${$fruitname}";
$fruitname = “apples”;
echo "number of {$fruitname}: ${$fruitname}";

• These are very useful when dealing with loops


and arrays.

August 2006 13
PHP Constants

• PHP Constants can’t have their values altered,


like most programming languages. You
create constants with the define function,
giving it the name of the constant and the
value you want to assign.

define ("pi", 3.1415926535);

The name of the constant is always quoted,


but the value you assign to the constant is
only quoted if it is a string literal. When you
use a constant name in code you don’t
precede it with a $.

• <?php
define ("pi",3.14152926535);
echo "The constant pi holds ", pi, "<br />";
?>
• Don’t confuse php by defining constants that
are already php key words, like

and default die foreach function

see page 29 in your text.

August 2006 14
PHP Data Types

• boolean
• integer
• float
• string
• array
• object
• resource
• NULL

• PHP is dynamically typed, the interpreter


determines the type of a variable and will
change the type as needed.

$variable = "a string";


$variable = 1.2345;
$variable = TRUE;
• When you mix types in an assignment
statement PHP will attempt to coerce and
perform the operation.
$var = 2 + "8 apples"; $var is now 10;

August 2006 15
TYPE CASTS

• PHP provides built-in function that cast


variables to a particular type.

$variable = "22";

$intvar = (integer) $variable;

$floatvar = (float) $variable;

$stringvar = (string) $variable;

• Values that will be converted to the boolean


false value.

FALSE
0
0.0
"" "0"
array with 0 elements
NULL

August 2006 16

You might also like