KEMBAR78
It 205 Week 1 3 1 | PDF | Php | Variable (Computer Science)
0% found this document useful (0 votes)
9 views43 pages

It 205 Week 1 3 1

for week 1

Uploaded by

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

It 205 Week 1 3 1

for week 1

Uploaded by

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

INTEGRATIVE

PROGRAMMING
TECHNOLOGIES 1
IT 205 - WEEK 1-2
LEARNING OBJECTIVES
LO 1. Discuss the basics of PHP syntax and structure
LO 2. Develop Dynamic Web pages with PHP
LO 3. Utilize PHP Functions
CHAPTER I : INTRODUCTION TO PHP
TOPICS COVERED:

1. Evaluation of PHP
2. PHP Basic Syntax
3. Defining Variable and Constant
4. PHP Data Type
5. PHP Operator
6. PHP Expression
Evaluation of PHP
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP
code
PHP code is executed on the server, and the result is returned
to the browser as plain HTML
PHP files have extension ".php"
What can PHP do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the
server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS
X, etc.)
PHP is compatible with almost all servers used today (Apache,
IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource:
www.php.net
PHP is easy to learn and runs efficiently on the server side
PHP Install
To start using PHP, you can:
F̀ind a web host with PHP and MySQL support
Install a web server on your own PC, and then install PHP
and MySQL
Set Up PHP on Your Own PC
However, if your server does not support PHP, you must:
install a web server
install PHP
install a database, such as MySQL
The official PHP website (PHP.net) has installation instructions for
PHP: http://php.net/manual/en/install.php
PHP Online Compiler / Editor
With w3schools' online PHP compiler, you can edit PHP code, and
view the result in your browser.

https://www.w3schools.com/php/phptryit.asp?
filename=tryphp_compiler
PHP Syntax
A PHP script is executed on the server, and the plain HTML
result is sent back to the browser.

A PHP script can be placed anywhere in the document.


A PHP script starts with <?php and ends with ?>:
<?php// PHP code goes here?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP
scripting code.
Below, we have an example of a simple PHP file, with a PHP
script that uses a built-in PHP function "echo" to output the
text "Hello World!" on a web page:
PHP Case Sensitivity
PHP Case Sensitivity
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions,
and user-defined functions are not case-sensitive.
In the example below, all three echo statements below are equal
and legal:
Example: ECHO is the same as echo;

Note: However; all variable names are


case-sensitive!
PHP Case Sensitivity
Look at the example below; only the first statement will display the
value of the $color variable! This is because $color, $COLOR, and
$coLOR are treated as three different variables:

Example:
$COLOR is not same as $color:
PHP Comments
A comment in PHP code is a line that is not executed as a part of
the program. Its only purpose is to be read by someone who is
looking at the code.
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have
experienced coming back to their own work a year or two later
and having to re-figure out what they did. Comments can
remind you of what you were thinking when you wrote the code
Leave out some parts of your code
PHP supports several ways of commenting:
PHP Example
Syntax for comments in PHP code:
PHP Variables
Variables are "containers" for storing information.
In PHP, a variable starts with the $ sign, followed by the name of
the variable:

Note: Unlike other programming


languages, PHP has no command for
declaring a variable. It is created the
moment you first assign a value to it.
PHP Variables
A variable can have a short name (like $x and $y) or a more descriptive
name ($age, $carname, $total_volume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the
variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different
variables)
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
Output Variables
The following example will produce the same output as the example
above:
Output Variables
The following example will output the sum of two variables:
PHP is a loosely typed language
In the example above, notice that we did not have to tell PHP which data
type the variable is.
PHP automatically associates a data type to the variable, depending on
its value. Since the data types are not set in a strict sense, you can do
things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives an option to specify
the data type expected when declaring a function, and by enabling the
strict requirement, it will throw a "Fatal Error" on a type mismatch.
You will learn more about strict and non-strict requirements, and data
type declarations in the PHP Functions chapter.
Variable Types
PHP has no command for declaring a variable, and the data type depends
on the value of the variable.
Supported Variable Types in PHP
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource
Get the Type
To get the data type of a variable, use the var_dump() function.
Assign String to a Variable
How to assign string to a Variable?

Code here:

Note: String variables can be declared either by using double or single


quotes, but you should be aware of the differences.
Assign Multiple Values
You can assign the same value to multiple variables in one line:
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can
be referenced/used.
PHP has three different variable scopes:
local
global
static
Global Scope
A variable declared outside a function has a GLOBAL SCOPE and can
only be accessed outside a function:
Local Scope
A variable declared within a function has a LOCAL SCOPE and can
only be accessed within that function:
PHP the Global keyword
The global keyword is used to access a global variable from within a
function.
To do this, use the global keyword before the variables (inside the
function):
PHP echo and print Statements
With PHP, there are two basic ways to get output: echo and print.
In this tutorial we use echo or print in almost every example. So, this
chapter contains a little more info about those two output statements.

echo and print are more or less the same. They are both used to
output data to the screen.

The differences are small: echo has no return value while print has a
return value of 1 so it can be used in expressions. echo can take
multiple parameters (although such usage is rare) while print can take
one argument. echo is marginally faster than print.
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or
echo().

Display Text
The following example shows how to output text with the echo
command (notice that the text can contain HTML markup):
Display Variables
The following example shows how to output text and variables with the
echo statement:
Data Type Definition

A string is a sequence of characters, like "Hello world!".


String A string can be any text inside quotes. You can use single or double
quotes:

An integer data type is a non-decimal number between


Integer -2,147,483,648 and 2,147,483,647.

A float (floating point number) is a number with a decimal point or a


Float number in exponential form.

Boolean A Boolean represents two possible states: TRUE or FALSE.


Data Type Definition

Array An array stores multiple values in one single variable.

Classes and objects are the two main aspects of object-oriented


Object programming.

Null Null is a special data type which can have only one value: NULL.
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction,
multiplication etc.
Assignment Operators
The PHP assignment operators are used with numeric values to write a
value to a variable.
The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the
right.
Comparison Operators
The PHP comparison operators are used to compare two values
(number or string):
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's
value.
PHP Logical Operators
The PHP logical operators are used to combine conditional
statements.
PHP String Operators
PHP has two operators that are specially designed for strings.
PHP Array Operators
The PHP array operators are used to compare arrays.

You might also like