SlideShare une entreprise Scribd logo
1  sur  36
Introduction in PHP
Table of contents
• What is PHP?
• Syntax
• Variables, variable types
• Basic functions
• Some predefined variables
• Strings escaping
• PHP – advantages and disadvantages
What is PHP?
Syntax
• The PHP code starts with <?php and ends
with ?>
 Depending on server configuration may also
start with <? (Short style) – but this is bad
practice!
 In terms of XML the <?php - ?> part is called
"processing instruction"
• PHP follows the Perl syntax
 Simplified
 Procedural (Now has OOP too)
 Similar to C and Java
• PHP Script contains one or more statements
Statement are handed to the PHP Preprocessor
one by one
Each statement ends in semicolon ";"
• Our first script contains only one statement:
– call of the function print
<?php
print ("Hello PHP!"); // this is the statement
?>
• PHP script can contain unlimited number of
statements
• Some function can be called without brackets
• You can add comments to the code
Starting with "//", "#" or block in "/*" and "*/"
Only "/*" – "*/" can be used over several lines
Comments are NOT executed
<?php
print "<div>";
print "Hello PHP!";
print "</div>";
?>
• Short opening tag <?=
Forces the result of the expression to be printed
to the browser
Similar to print
Allowed to omit ending ";"
<html>
<head><title>Hello world page</title></head>
<body>
<?="Hello PHP!" ?>
</body>
</html>
Variables
• All variables in PHP start with $ (Perl style)
• PHP is "type-less" language
Variables are not linked with type – they can store value
with different types
No int a = 5; Just $a = 5;
• Each variable is declared when it's first assigned value
This leads to problems due to typing mistakes!
The type of the value determines the type of the variable
<?php // declare string variable $output
$output = "<div>Hello PHP!</div>";
print $output;
?>
• Possible PHP Variable Types are:
Numeric (real or integer)
• The decimal separator is dot ".", not comma
","
Boolean (true or false)
• PHP defines the constants as true, TRUE, True
and false, FALSE, False
• Empty string, zero and some other values are
implicitly converted to "false" in boolean
expressions
– May cause problems when boolean not used
properly
• String values
• Strings may be in single or double quotes
• Start and end quote type should match
• Difference between two types of quotes is
the escape sequences
<?
$output1 = "Hello PHP!";
$output2 = 'Hello again!';
?>
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
• Arrays are aggregate values –
combination of values, each assigned a
key in the array
PHP supports associative arrays – keys may be
numeric, strings or any other scalar data types
Keys must be unique across the array
Values in the array may be with different types
PHP Arrays are dynamic – they don’t require
explicit size when created
• PHP Array is declared with keyword array
"=>" means "points to"
If keys are not supplied they are assigned
automatically, starting from 0
<?
// simple array
$arr = array ("a", "b", 7);
// this produces $arr[0], $arr[1] and $arr[2]
// whit values respectively "a", "b" and 7
$arr2 = array ("one" => 1, "two" => 2);
// this produces $arr2["one"] and $arr2["two"]
// whit values respectively 1 and 2
?>
• We access value in the array with "[" and "]"
containing the key
• Arrays are flexible and types of values and
keys may be mixed
<?
$arr = array ("a", "b", 7, "one" => 1, "two" => 2, "other" => array(1,2,3));
// keys types may be mixed:
// $arr[0] will be "a" and $arr["one"] will be 1
// $arr["other"] is also array
// $arr["other"][0]" is 1
print $arr["other"][2]; // will output 3
?>
• In PHP there is special value (null) that means
that the variable has no value
– It is used to express the absence of any data type
– Different from "undefined" variable!
– Different from empty string or zero
<?
$null_variable = null;
?>
• PHP supports "object" variable type
Will be explained further in the OOP lecture
• "Resource" variable type
The resource type means the variable is holding
reference to resource or data, external to your
script
• Example – opened file, database connection,
etc
• PHP expressions are similar to C
"=" - assigning value to variable
+, -, /, *, % - arithmetic operations
==, <=, >=, !=, <, > - comparison
+=, -=, /=, *=, %=, ++, --, etc – prefix/postfix
operators
( and ) – for expressions combining
&, |, >>, <<, ^, ~ - bitwise operators
• String operators
"." (period) – string concatenating
• ===, !== comparison
• different from ==, !=
"10"==10 will produce true, while "10"===10 will
produce false
Strict comparison – $a === $b :
TRUE if $a is equal to $b, and they are of the same
type.
Note: Assignment of value to variable returns as
result the value being assigned
We can have $a = $b = $c = 7;
• In PHP constants are defined with the define
function
Cannot change value
Doesn't start with $
Can hold any scalar value
<?
define ('CONSTANT_NAME', 123);
// from here on CONSTANT_NAME will have value 123
print CONSTANT_NAME; // will output 123
?>
Basic Functions
Phpinfo
Live Demo
• We already know print
Similar to print is echo
print_r(array) – pints array with keys and values
detailed
• phpinfo() – Produces complete page
containing information for the server, PHP
settings, installed modules, etc
<?
echo "123"; // will output 123 to the browser
?>
Predefined Variables
• PHP provides a lot predefined variables and
constants
__FILE__, __LINE__, __FUNCTION__,
__METHOD__, __CLASS__ - contain debug info
PHP_VERSION, PHP_OS, PHP_EOL,
DIRECTORY_SEPARATOR, PHP_INT_SIZE and
others are provided for easy creating cross-
platform applications
• $_SERVER – array, holding information from
the web server – headers, paths and script
locations
DOCUMENT_ROOT – the root directory of the
site in the web server configuration
SERVER_ADDRESS, SERVER_NAME,
SERVER_SOFTWARE, SERVER_PROTOCOL
REMOTE_ADDR, REMOTE_HOST, REMOTE_PORT
PHP_AUTH_USER, PHP_AUTH_PW,
PHP_AUTH_DIGEST
And others
• $_GET, $_POST, $_COOKIE arrays hold the
parameters from the URL, from the post data
and from the cookies accordingly
• $_FILES array holds information for
successfully uploaded files over multipart
post request
• $_SESSION array holds the variables, stored
in the session
• PHP supports $$ syntax- variable variables
The variable $str1 is evaluated as 'test' and so
$$str1 is evaluated as $test
<?
$str1 = 'test';
$test = 'abc';
echo $$str1; // outputs abc
?>
Strings Escaping
• Special chars in stings are escaped with
backslashes (C style)
The escape sequences for double quoted string:
• n – new line (10 in ASCII)
• r – carriage return (13 in ASCII)
• t – horizontal tab
• v – vertical tab
•  - backslash
• $ - dollar sign
• " – double quote
$str1 = "this is "PHP"";
• Single-quoted strings escape the same way
Difference is that instead of " you need ' to
escape the closing quotes
No other escaping sequences will be expanded
• In both single and double quoted strings,
backslash before any other character will be
printed too!
$str1 = 'Arnold once said: "I'll be back"';
• Double quoted strings offer something more:
Variables in double-quoted strings are evaluated
• Note on arrays:
$saying = "I'll be back!";
$str1 = "Arnold once said: $saying";
// this will output:
// Arnold once said: I'll be back!
$sayings = array ('arni' => "I'll be back!");
$str1 = "Arnold once said: ${sayings['arni']}";
Advantages and Disadvantages
• Advantages
Easy to learn, open source, multiplatform and
database support, extensions, community and
commercial driven.
Considered to be one of the fastest languages
• Disadvantages
Too loose syntax – risk tolerant, poor error
handling, poor OOP (before version 6 a lot things
are missing!)
• Resources
– http://php-uroci.devbg.org/
– http://academy.telerik.com/
Introduction in php

Contenu connexe

Tendances

php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in functiontumetr1
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021Ayesh Karunaratne
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in phpAshish Chamoli
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Php server variables
Php server variablesPhp server variables
Php server variablesJIGAR MAKHIJA
 
PHP Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance TriviaNikita Popov
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 

Tendances (20)

Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
Syntax
SyntaxSyntax
Syntax
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
Php functions
Php functionsPhp functions
Php functions
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Php server variables
Php server variablesPhp server variables
Php server variables
 
PHP Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance Trivia
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Subroutines
SubroutinesSubroutines
Subroutines
 

Similaire à Introduction in php

Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Michelangelo van Dam
 
Php basics
Php basicsPhp basics
Php basicshamfu
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptxrani marri
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHPBUDNET
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - phpHung-yu Lin
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfpkaviya
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learningASIT Education
 
php fundamental
php fundamentalphp fundamental
php fundamentalzalatarunk
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of phppooja bhandari
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 

Similaire à Introduction in php (20)

Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
Php basics
Php basicsPhp basics
Php basics
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
05php
05php05php
05php
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learning
 
Prersentation
PrersentationPrersentation
Prersentation
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
Php basics
Php basicsPhp basics
Php basics
 
Php
PhpPhp
Php
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 

Plus de Bozhidar Boshnakov

PMG Gabrovo - Web Development Level 0 - Introduction
PMG Gabrovo - Web Development Level 0 - IntroductionPMG Gabrovo - Web Development Level 0 - Introduction
PMG Gabrovo - Web Development Level 0 - IntroductionBozhidar Boshnakov
 
QA Challange Accepted - How and why we should use Behat?
QA Challange Accepted - How and why we should use Behat?QA Challange Accepted - How and why we should use Behat?
QA Challange Accepted - How and why we should use Behat?Bozhidar Boshnakov
 
Как да направим живота си по - лесен с добър QA подход
Как да направим живота си по - лесен с добър QA подходКак да направим живота си по - лесен с добър QA подход
Как да направим живота си по - лесен с добър QA подходBozhidar Boshnakov
 

Plus de Bozhidar Boshnakov (9)

Mission possible - revival
Mission possible - revivalMission possible - revival
Mission possible - revival
 
Web fundamentals 2
Web fundamentals 2Web fundamentals 2
Web fundamentals 2
 
Web fundamentals - part 1
Web fundamentals - part 1Web fundamentals - part 1
Web fundamentals - part 1
 
PMG Gabrovo - Web Development Level 0 - Introduction
PMG Gabrovo - Web Development Level 0 - IntroductionPMG Gabrovo - Web Development Level 0 - Introduction
PMG Gabrovo - Web Development Level 0 - Introduction
 
QA Challange Accepted - How and why we should use Behat?
QA Challange Accepted - How and why we should use Behat?QA Challange Accepted - How and why we should use Behat?
QA Challange Accepted - How and why we should use Behat?
 
DrupalCamp Sofia 2015
DrupalCamp Sofia 2015DrupalCamp Sofia 2015
DrupalCamp Sofia 2015
 
BDD, Behat & Drupal
BDD, Behat & DrupalBDD, Behat & Drupal
BDD, Behat & Drupal
 
Automation in Drupal
Automation in DrupalAutomation in Drupal
Automation in Drupal
 
Как да направим живота си по - лесен с добър QA подход
Как да направим живота си по - лесен с добър QA подходКак да направим живота си по - лесен с добър QA подход
Как да направим живота си по - лесен с добър QA подход
 

Dernier

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Dernier (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Introduction in php

  • 2. Table of contents • What is PHP? • Syntax • Variables, variable types • Basic functions • Some predefined variables • Strings escaping • PHP – advantages and disadvantages
  • 5. • The PHP code starts with <?php and ends with ?>  Depending on server configuration may also start with <? (Short style) – but this is bad practice!  In terms of XML the <?php - ?> part is called "processing instruction" • PHP follows the Perl syntax  Simplified  Procedural (Now has OOP too)  Similar to C and Java
  • 6. • PHP Script contains one or more statements Statement are handed to the PHP Preprocessor one by one Each statement ends in semicolon ";" • Our first script contains only one statement: – call of the function print <?php print ("Hello PHP!"); // this is the statement ?>
  • 7. • PHP script can contain unlimited number of statements • Some function can be called without brackets • You can add comments to the code Starting with "//", "#" or block in "/*" and "*/" Only "/*" – "*/" can be used over several lines Comments are NOT executed <?php print "<div>"; print "Hello PHP!"; print "</div>"; ?>
  • 8. • Short opening tag <?= Forces the result of the expression to be printed to the browser Similar to print Allowed to omit ending ";" <html> <head><title>Hello world page</title></head> <body> <?="Hello PHP!" ?> </body> </html>
  • 10. • All variables in PHP start with $ (Perl style) • PHP is "type-less" language Variables are not linked with type – they can store value with different types No int a = 5; Just $a = 5; • Each variable is declared when it's first assigned value This leads to problems due to typing mistakes! The type of the value determines the type of the variable <?php // declare string variable $output $output = "<div>Hello PHP!</div>"; print $output; ?>
  • 11. • Possible PHP Variable Types are: Numeric (real or integer) • The decimal separator is dot ".", not comma "," Boolean (true or false) • PHP defines the constants as true, TRUE, True and false, FALSE, False • Empty string, zero and some other values are implicitly converted to "false" in boolean expressions – May cause problems when boolean not used properly
  • 12. • String values • Strings may be in single or double quotes • Start and end quote type should match • Difference between two types of quotes is the escape sequences <? $output1 = "Hello PHP!"; $output2 = 'Hello again!'; ?> $s = "dollars"; echo 'This costs a lot of $s.'; // This costs a lot of $s. echo "This costs a lot of $s."; // This costs a lot of dollars.
  • 13. • Arrays are aggregate values – combination of values, each assigned a key in the array PHP supports associative arrays – keys may be numeric, strings or any other scalar data types Keys must be unique across the array Values in the array may be with different types PHP Arrays are dynamic – they don’t require explicit size when created
  • 14. • PHP Array is declared with keyword array "=>" means "points to" If keys are not supplied they are assigned automatically, starting from 0 <? // simple array $arr = array ("a", "b", 7); // this produces $arr[0], $arr[1] and $arr[2] // whit values respectively "a", "b" and 7 $arr2 = array ("one" => 1, "two" => 2); // this produces $arr2["one"] and $arr2["two"] // whit values respectively 1 and 2 ?>
  • 15. • We access value in the array with "[" and "]" containing the key • Arrays are flexible and types of values and keys may be mixed <? $arr = array ("a", "b", 7, "one" => 1, "two" => 2, "other" => array(1,2,3)); // keys types may be mixed: // $arr[0] will be "a" and $arr["one"] will be 1 // $arr["other"] is also array // $arr["other"][0]" is 1 print $arr["other"][2]; // will output 3 ?>
  • 16. • In PHP there is special value (null) that means that the variable has no value – It is used to express the absence of any data type – Different from "undefined" variable! – Different from empty string or zero <? $null_variable = null; ?>
  • 17. • PHP supports "object" variable type Will be explained further in the OOP lecture • "Resource" variable type The resource type means the variable is holding reference to resource or data, external to your script • Example – opened file, database connection, etc
  • 18. • PHP expressions are similar to C "=" - assigning value to variable +, -, /, *, % - arithmetic operations ==, <=, >=, !=, <, > - comparison +=, -=, /=, *=, %=, ++, --, etc – prefix/postfix operators ( and ) – for expressions combining &, |, >>, <<, ^, ~ - bitwise operators
  • 19. • String operators "." (period) – string concatenating • ===, !== comparison • different from ==, != "10"==10 will produce true, while "10"===10 will produce false Strict comparison – $a === $b : TRUE if $a is equal to $b, and they are of the same type. Note: Assignment of value to variable returns as result the value being assigned We can have $a = $b = $c = 7;
  • 20. • In PHP constants are defined with the define function Cannot change value Doesn't start with $ Can hold any scalar value <? define ('CONSTANT_NAME', 123); // from here on CONSTANT_NAME will have value 123 print CONSTANT_NAME; // will output 123 ?>
  • 23. • We already know print Similar to print is echo print_r(array) – pints array with keys and values detailed • phpinfo() – Produces complete page containing information for the server, PHP settings, installed modules, etc <? echo "123"; // will output 123 to the browser ?>
  • 25. • PHP provides a lot predefined variables and constants __FILE__, __LINE__, __FUNCTION__, __METHOD__, __CLASS__ - contain debug info PHP_VERSION, PHP_OS, PHP_EOL, DIRECTORY_SEPARATOR, PHP_INT_SIZE and others are provided for easy creating cross- platform applications
  • 26. • $_SERVER – array, holding information from the web server – headers, paths and script locations DOCUMENT_ROOT – the root directory of the site in the web server configuration SERVER_ADDRESS, SERVER_NAME, SERVER_SOFTWARE, SERVER_PROTOCOL REMOTE_ADDR, REMOTE_HOST, REMOTE_PORT PHP_AUTH_USER, PHP_AUTH_PW, PHP_AUTH_DIGEST And others
  • 27. • $_GET, $_POST, $_COOKIE arrays hold the parameters from the URL, from the post data and from the cookies accordingly • $_FILES array holds information for successfully uploaded files over multipart post request • $_SESSION array holds the variables, stored in the session
  • 28. • PHP supports $$ syntax- variable variables The variable $str1 is evaluated as 'test' and so $$str1 is evaluated as $test <? $str1 = 'test'; $test = 'abc'; echo $$str1; // outputs abc ?>
  • 30. • Special chars in stings are escaped with backslashes (C style) The escape sequences for double quoted string: • n – new line (10 in ASCII) • r – carriage return (13 in ASCII) • t – horizontal tab • v – vertical tab • - backslash • $ - dollar sign • " – double quote $str1 = "this is "PHP"";
  • 31. • Single-quoted strings escape the same way Difference is that instead of " you need ' to escape the closing quotes No other escaping sequences will be expanded • In both single and double quoted strings, backslash before any other character will be printed too! $str1 = 'Arnold once said: "I'll be back"';
  • 32. • Double quoted strings offer something more: Variables in double-quoted strings are evaluated • Note on arrays: $saying = "I'll be back!"; $str1 = "Arnold once said: $saying"; // this will output: // Arnold once said: I'll be back! $sayings = array ('arni' => "I'll be back!"); $str1 = "Arnold once said: ${sayings['arni']}";
  • 34. • Advantages Easy to learn, open source, multiplatform and database support, extensions, community and commercial driven. Considered to be one of the fastest languages • Disadvantages Too loose syntax – risk tolerant, poor error handling, poor OOP (before version 6 a lot things are missing!)