0% found this document useful (0 votes)
12 views

Introduction To PHP7 CH 1 4

The document provides an introduction to PHP and SQL. It discusses topics such as what PHP is, PHP files, variables, strings, numbers, operators, and comments. It also discusses sending data to the browser and differences between single and double quotation marks.

Uploaded by

s7jkj8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Introduction To PHP7 CH 1 4

The document provides an introduction to PHP and SQL. It discusses topics such as what PHP is, PHP files, variables, strings, numbers, operators, and comments. It also discusses sending data to the browser and differences between single and double quotation marks.

Uploaded by

s7jkj8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to PHP

Insert the Subtitle of Your Presentation


TOPICS IN THIS CHAPTER:
01 What is PHP & SQL 02 Sending Data to the Browser like ASP

03 Writing Comments 04 What Are Variables?

06 Introducing Numbers &


05
Introducing Strings & Concatenating Strings Introducing Constants
PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)

07 Single vs. Double Quotation Marks

PHP&SQL
PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
What is PHP?
01 PHP stands for PHP: Hypertext 02 PHP is a server-side scripting language, like
Preprocessor ASP

03 PHP scripts are executed on the server 04 PHP is an open source software is free to
DTD download and use

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

PHP&SQL
What is PHP File?
01 PHP files can contain text, HTML tags and
scripts

02 PHP files are returned to the browser as plain


HTML DTD

03
PHP files have a file extension of ".php", ".php3",
or ".phtml"

PHP&SQL
What is MySQL?
What is MYSQL?
01 MySQL is a database server. 02 MySQL compiles on several platforms

03
MySQL is ideal for both small and large 04 MySQL supports standard SQL. MySQL
DTD
applications and use compiles on a number of platforms

PHP&SQL
Sending Data to the
Browser

To create dynamic web sites with PHP, you


must know how to send data to the
browser. PHP has a number of built-in
functions for this purpose; the most
common are echo and print. I tend to (16)
favor echo:

echo 'Hello, world!'; echo "What's


new?";
You could use print instead if you prefer (the
name more obviously indicates what it
does):
print 'Hello, world!'; print "What's new?"; PHP&SQL​
//Using echo
<?php
echo 'This was generated
using PHP!';
?>

//Using print
<?php
print 'Hello, world!';
print "What's new?";

?>
PHP&SQL​
Writing
Comments

HTML you can add comments using special tags:


In

<!-- Comment goes here. -->


PHP comments are different in that they aren’t sent to the
browser at all, meaning they won’t be viewable to the end
user, even when looking at the HTML source.
PHP supports three comment syntaxes.
Writing Comments
PHP supports three comment syntaxes.

01 02 03
The first uses what’s The second uses two third style allows
called the pound, hash, or slashes: comments to run over
number: multiple lines:
A /*This is a longer
# This is a comment. // This is also a comment
comment. that spans two
lines. */
What are Variables
What are variables
Variables are containers used to temporarily store values.
In

These values can be numbers, text, or much more complex


data.

A variable’s name must start with a dollar sign ($)—for


example, $name.

The variable’s name can contain a combination of letters,


numbers, and the underscore—for example, $my_report1.

The first character after the dollar sign must be either a letter
or an underscore (it cannot be a number).

Variable names in PHP are case-sensitive! This is a very


important rule. It means that $name and $Name are different
variables.
What are variables
Before getting into this script, there are two more things you
should know. First, variables can be assigned values using
the equals sign (=),
also called the assignment operator. Second, to display the
value of a variable, you
can print the variable without quotation marks: print
$some_var;

Or variables can be printed within double quotation marks:


print "Hello, $name";
You cannot print variables within single quotation marks:

print 'Hello, $name';


// This won't work!
String variables are created and their values
are sent to the browser in this script.

<?php # Script 1.6 - strings.php

// Create the variables:


$first_name = 'Haruki';
$last_name = 'Murakami';
$book = 'Kafka on the Shore';
// Print the values:
echo "<p>The book <em>$book</em> was
written by $first_name
$last_name.</p>";
?>
This script prints three of PHP's
<?php # Script 1.5 - predefined.php many predefined variables.

// Create a shorthand version of the variable names:


$file = $_SERVER['SCRIPT_FILENAME'];
$user = $_SERVER['HTTP_USER_AGENT'];
$server = $_SERVER['SERVER_SOFTWARE'];

// Print the name of this script:


echo "<p>You are running the file:<br><strong>$file</strong>.</p>\n";

// Print the user's information:


echo "<p>You are viewing this page using:<br><strong>$user</strong></p>\
n";
// Print the server's information:
echo "<p>This server is running:<br><strong>$server</strong>.</p>\n";
?>
Concatenating
city= 'Seattle'; Strings:
$state = 'Washington';
$address = $city . $state;

The $address variable now has the


value SeattleWashington, which
almost achieves the desired
result (Seattle, Washington). To
improve upon this, you could
write:

$address = $city . ', ' . $state;

so that a comma and a space


are concatenated to the
variables as well.
Concatenation gives you the ability to
append more characters onto a string.
<?php # Script 1.7 - concat.php

// Create the variables:


$first_name = 'Melissa';
$last_name = 'Bank';
$author = $first_name . ' ' .
$last_name;

$book = 'The Girls\' Guide to Hunting and Fishing';

//Print the values:


echo "<p>The book <em>$book</em> was
written by $author.</p>";

?>
Operators
The Different operators used in PHP are:
+ , - , *, / , %, ++,-- &&,||,!
Arithmetic Logical
Operators Operators

= , +=, -=, *=,/=,%= ==,!=,<>,>,<,>=,<=


Assignment Comparison
Operators Operators
Arithmetic Operators
The numbers.php script performs basic
mathematical calculations, like those used
<?php # Script 1.8 - numbers.php
in an e-commerce application.
// Set the variables:
$quantity = 30; // Buying 30 widgets.
$price = 119.95;
$taxrate = .05; // 5% sales tax.
// Calculate the total:
$total = $quantity * $price;
$total = $total + ($total * $taxrate); // Calculate and add the tax.
// Format the total:
$total = number_format ($total, 2);

// Print the results:


echo '<p>You are purchasing <strong>' .
$quantity . '</strong> widget(s) at a cost of <strong>$' .
$price . '</strong> each. With tax, the total comes to
<strong>$' . $total . '</strong>.</p>';

?>
Single vs. Double
Quotation Marks
In PHP, it’s important to understand how single quotation marks
differ from double quotation marks. With echo and print, or when
assigning values to strings, you can use either, as in the examples
used so far. But there is a key difference between the two types
of quotation marks and when you should use which. You’ve seen
this difference already, but it’s an important enough concept to
merit more discussion.
<?php # Script 1.10 - quotes.php

// Set the variables:


$quantity = 30; // Buying 30 widgets.
$price = 119.95;
$taxrate = .05; // 5% sales tax.

// Calculate the total.


$total = $quantity * $price;
$total = $total + ($total * $taxrate); // Calculate and add the tax.

// Format the total:


$total = number_format ($total, 2);

// Print the results using double quotation marks:


echo "<h3>Using double quotation
marks:</h3>"; This echo statement is used to
echo "<p>You are purchasing <strong>$quantity</strong> widget(s) at a cost of <strong>\$
$price </strong> each. With tax, the total comes to <strong>\$$total</strong>. highlight the difference between
</p>\n"; using single or double quotation
marks. It will not work as
// Print the results using single quotation marks:
echo '<h3>Using single quotation marks:</h3>'; desired, and the resulting page
echo '<p>You are purchasing <strong>$quantity</strong> widget(s) at a cost of will show you exactly what does
<strong>\$$price </strong> each. With tax, the total comes to <strong>\$$total</strong>.
</p>\n'; happen instead.

?>
Review on the Topics
Discussed:
01 What tags are used to surround PHP 02 What extension should a PHP file have?
code? What does a page’s encoding refer to? like
ASP

03 What PHP functions, or language constructs, 04 How does using single versus double
can you use to send data to the browser? quotation marks differ in creating or printing
strings?
What character do all variable names begin 06 What is the operator?
05
with? What characters can come next? What
other characters can be used in a variable’s
name?Pupports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
07 What is the concatenation operator? What is

PHP&SQL
the concatenation assignment operator?PHP
supports many databases (MySQL, Informix, Oracle,
Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
The End
Insert the Subtitle of Your Presentation

You might also like