Download Introduction to PHP - Lecture - PHP - Prof G H Haldar and more Lecture notes Programming Languages in PDF only on Docsity!
PHP
What is PHP?
PHP stands for P HP: H ypertext P reprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, etc.)
PHP is an open source software (OSS)
PHP is free to download and use
What do You Need?
Download PHP
Download PHP for free here: http://www.php.net/downloads.php
Download MySQL Database
Download MySQL for free here:
http://www.mysql.com/downloads/index.html
Download Apache Server
Download Apache for free here: http://httpd.apache.org/download.cgi
PHP Syntax
Basic PHP Syntax
A PHP file normally contains HTML tags, just like an HTML file, and
some PHP scripting code.
Comments in PHP
// ----to make a single-line comment or
/* */ --- multiple lines
PHP Variables
All variables in PHP start with a $ sign symbol.
$var_name = value;
variable does not need to be declared before being set
declared automatically when you use it
Variable Naming Rules
A variable name must start with a letter or an underscore "_"
A variable name can only contain alpha-numeric characters and
underscores (a-Z, 0-9, and _ )
A variable name should not contain spaces (use underscore)
Strings in PHP
used for values that contains character strings
The Concatenation Operator (.)
concatenate two variables together
strlen() function
used to find the length of a string
strpos() function
used to search for a string or character within a string
echo strpos("Hello world!","world");
Strings in PHP (^) http://www.w3schools.com/php/php_ref_string.asp
PHP Operators
PHP constant
like a variable,
is a temporary placeholder in memory that holds a value.
Unlike variables, the value of a constant never changes.
PHP constants unlike variables do not begin with the "$" sign.
When a constant is declared, the define() function is used and requires the name of the
constant and the value you want to give the constant
Constants can be assigned the following types of data:
Integers - whole numbers or numbers without decimals.
Floating-point numbers (also known as floats or doubles) -
Strings - text or numeric information.
PHP constant
define("STRING_CONSTANT", "This is my string.");
define("NUMERIC_CONSTANT", 5);
Flow Control Statements
Conditional Statements
1. "if ... elseif ... else" statements.
if ( condition )
code to be executed if condition is true;
else code to be executed if condition is false;
2. "switch ... case ... default" statements.
If you want to select one of many blocks of code to be executed, use the Switch
statement.
The switch statement is used to avoid long blocks of if..elseif..else code.
After a code is executed, break is used to stop the code from running into the
next case
Flow Control Statements
Looping
while - loops through a block of code if and as long as a specified condition
is true
do...while - loops through a block of code once, and then repeats the loop as
long as a special condition is true. will execute a block of code at least once
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
foreach ( array as value ) { code to be executed; }
PHP Arrays
2. Associative Arrays
each ID key is associated with a value.
we can use the values as keys and assign values to them
E.g. 1
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
E.g. 2
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
PHP Arrays
3. Multidimensional Arrays
(^) each element in the main array can also be an array. (^) And each element in the sub-array can be an array, and so on. (^) E.g. in practical