7 PHP 1
7 PHP 1
EMY HARYATMI
WHAT IS PHP?
• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code are executed on the server, and the result is returned to the browser as plain
HTML
• PHP files have extension ".php"
WHAT CAN PHP DO?
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any
text, such as XHTML and XML.
WHY PHP?
• In PHP, all 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 legal (and equal):
• 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)
• The PHP echo statement is often used to output data to the screen.
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 converts the variable to the correct data type, depending on its value.
• In other languages such as C, C++, and Java, the programmer must declare the name and
type of the variable before using it.
PHP VARIABLES 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:
• 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
• Display Variable
THE PHP PRINT STATEMENT
• The print statement can be used with or without parentheses: print or print().
• Display Text
• Display Variable
PHP DATA TYPES
• Variables can store data of different types, and different data types can do different things.
• PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP STRING
• The special resource type is not an actual data type. It is the storing of a reference to
functions and resources external to PHP.
• A common example of using the resource data type is a database call.
PHP STRING FUNCTION
• A constant is an identifier (name) for a simple value. The value cannot be changed during
the script.
• A valid constant name starts with a letter or underscore (no $ sign before the constant
name).
• Note: Unlike variables, constants are automatically global across the entire script.
• Constants are like variables except that once they are defined they cannot be changed or
undefined.
CREATE A PHP CONSTANT
• Parameters:
• name: Specifies the name of the constant
• value: Specifies the value of the constant
• case-insensitive: Specifies whether the constant name should
be case-insensitive. Default is false
CONSTANTS ARE GLOBAL
• Constants are automatically global and can be used across the entire script.
PHP OPERATORS
Very often when you write code, you want to perform different actions for different
conditions.You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another code if that
condition is false
• if...elseif....else statement - executes different codes for more than two conditions
• switch statement - selects one of many blocks of code to be executed
• If Statement
• If … else statement
This is how it works: First we have a single expression n (most often a variable), that is
evaluated once. The value of the expression is then compared with the values for each case
in the structure. If there is a match, the block of code associated with that case is
executed. Use break to prevent the code from running into the next case automatically.
The default statement is used if no match is found.