SlideShare a Scribd company logo
PHP BasicsPrepared By: Mary Grace G. Ventura 
PHP Scripting BlockAlways starts with <?php and ends with ?><html><head><title>Hello World Script</title></head><body><?php			echo “<p>Hello World!</p>”?></body></html>
Using Simple StatementsSimple statements are an instruction to PHP to do one simple action.There are two basic statements to output text with PHP: echo and print. Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.
echo commandThe simplest use of echo is to print a string as argument, for example:echo “This will print in the user’s browser window.”;Or equivalentlyecho(“This will print in the user’s browser window.”);
Echo StatementYou can also give multiple arguments to the unparenthesized version of echo, separated by commas, as in:echo “This will print in the “, 		“user’s browser window.”;The parenthesized version, however, will not accept multiple arguments:echo (“This will produce a “, “PARSE ERROR!”);
Used of \n- newline				echo “line 1\n”;				echo “line 2”;Will producedline 1 line 2Used of <br />				echo “line 1<br />”;				echo “line 2”;Will producedline 1line 2Used of <br /> and \n
PHP Simple StatementsFollow These Rules:PHP statements end with a semicolon or the PHP ending tag. PHP doesn’t notice white space or the end of lines. It continues reading a statement until it encounters a semicolon or the PHP closing tag, no matter how many lines the statement spans.PHP statements may be written in either upper- or lowercase. In an echo statement, Echo, echo, ECHO, and eCHo are all the same to PHP.
Comments in PHPIn PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <?php		//This is a line comment			# or this one		/*		This is		a comment		block		*/?>
VariablesAre used for storing values such as numbers and strings so that it can be used several times in the script. “Symbolic Representation of a value”.Variables are identified and defined by prefixing their name with a dollar sign Example: $variable1, $variable2Variable names must start with a letter or underscore character (“_”)Variable names may contain letters, numbers, underscores, or dashes. There should be no spaces Variable names are CaSE- SeNSiTiVE$thisVar$ThisvAr
Example of Variable Names$item$Item$myVariable (camel case)$this_variable$this-variable$product3$_book$__bookPage
VariablesPHP variables are not declared explicitly instead they are declared automatically the first time they are used.It’s a loosely typed of language so we do not specify the data type of the variable.PHP automatically converts the variable to the correct data type such as string, integer or floating point numbers.
Illustration of a Variable Declaration
Example of Variable Variables$varName=“ace”;$$varName=1029;This is exactly equivalent to$ace=1029;
ConstantsAlways stay the same once definedConstants are defined with the define() function. Example: define(“VARIABLE1”, “value”);Constant names follow the same rules as variable namesConstants are typically named all in UPPERCASE but do not have to be.
Variables and Constants Ex:
ConstantsOne important difference between constants and variables is that when you refer to a constant, it does not have a dollar sign in front of it.If you want to use the value of a constant, use its name only.define(‘OILPRICE’,10);echo OILPRICE;
Sample Program for Constant Declaration<?php	define(“USER”,”Grace”);	echo “Welcome ” . USER;?>Output:Welcome Grace
IntegerAn integer is a plain-vanilla number like 75, -95, 2000,or 1.Integers can be assigned to variables, or they can be used in expressions, like so:$int_var = 12345;
Floating PointA floating-point number is typically a fractional number such as 12.5 or 3.149391239129.Floating point numbers may be specified using either decimal or scientific notation.Ex: $temperature = 56.89;
DoublesDoubles are floating-point numbers, such as:$first_double = 123.456;$second_double = 0.456$even_double = 2.0;Note that the fact that $even_double is a “round” number does not make it an integer. And the result of:$five = $even_double + 3;is a double, not an integer, even if it prints as 5. In almost all situations, however, you should feel free to mix doubles and integers in mathematical expressions, and let PHP sort out the typing.
BooleanThe simplest variable type in PHP, a Boolean variable simply specifies a true or false value.TRUE=1, FALSE=0Case-InsensitiveTrue, TRUE, true are all the same.Printing out Boolean values.echo true . “\n”; //prints trueecho false; //(none)1(none)
BooleanThe ff. are considered FALSE:Integers and floats zero(0)Empty String (“”)The string “0”Array with zero elementsNULLObject with zero member variablesEvery other value is considered TRUE.
NULLNull is a special value that indicates no value.Case-insensitiveNULL, null, NullNULL converts to boolean FALSE and integer zero.A variable is considered to be NULL if:It has been assigned to the constant NULLIt has not been set to any value yetIt has been unset<?php$a= NULL;echo $b; ?>
isset(), is_null()isset()Tests if a variable existsReturns FALSE if:Is set to NULLVariable has been unset()is_null()Determines if the given variable is set to NULL.Returns true if variable is NULL, FALSE otherwise.
empty()Determines if a variable is empty.The following values are considered empty:
isset() vs empty() vs is_null()
StringsA string is a sequence of characters, like 'hello' or 'abracadabra'. String values may be enclosed in either double quotes ("") or single quotes ('').$name1 = ‘Ervin';$name2 = ‘Grace’;
Singly Quoted StringsExcept for a couple of specially interpreted character sequences, singly quoted strings read in and store their characters literally.$literally = ‘My $variable will not print!\\n’;print($literally);produces the browser output:My $variable will not print!\\n
Doubly Quoted StringsStrings that are delimited by double quotes (as in “this”) are preprocessed in both the following two ways by PHP:Certain character sequences beginning with backslash (\) are replaced with special characters.Variable names (starting with $) are replaced with string representations of their values.
Escape Sequence 							Replacements Are:\n is replaced by the new line character \r is replaced by the carriage-return character\t is replaced by the tab character\$ is replaced by the dollar sign itself ($)\” is replaced by a single double-quote (“)\\ is replaced by a single backslash (\
A Note on String Values<?php$identity = 'James Bond';	$car = 'BMW';	// 	this would contain the string //	"James Bond drives a BMW"$sentence = "$identity drives a $car";	// this would contain the string //	"$identity drives a $car"$sentence = '$identity drives a $car';?>
A Note on String Values<?php// will cause an error due to// mismatched quotes$statement = 'It's hot outside';// will be fine$statement = 'It\'s hot outside';?>
Data ConversionIn PHP, the type of the variable depends on the value assigned to it.
TypecastingThere are circumstances in which you will want to control how and when individual variables are converted from one type to another.This is called typecastingTypecasting forces a variable to be evaluated as another typeThe name of the desired type is written in parentheses before the variable that is to be cast.
Typecasting-IntegersYou can typecast any variable to an integer using the (int) operator.Floats are truncated so that only their integer portion is maintained.echo (int) 99.99; 	//99Booleans are cast to either one or zero(int) TRUE == 1(int) FALSE == 0Strings are converted to their integer equivalentecho (int) “test 123” ; //0echo (int) “123”;echo (int) “123test”;NULL always evaluates to zero.
Typecasting BooleansData is cast to Boolean using the (bool) operatorecho (bool) “1”;Numeric values are always TRUE unless they evaluate to zeroStrings are always TRUE unless they are empty(bool) “FALSE” ==trueNull always evaluates to FALSE.
Typecasting- StringsData is typecast to a string using the (string) operator:echo (string) 123;Numeric values are converted to their decimal string equivalent:(string) 123.1 == “123.1”;Booleans evaluate to either “1” (TRUE) or an empty string (FALSE)NULL values evaluates to an empty string.
Gettype()Gets the type of a variableReturns “boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”.
settype()Sets the type of a variable“boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”
Activity
Ad

More Related Content

What's hot (20)

PHP Basics
PHP BasicsPHP Basics
PHP Basics
Bhaktaraz Bhatta
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
Deepak Rajput
 
php basics
php basicsphp basics
php basics
Anmol Paul
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
Mohammed Ilyas
 
Hack programming language
Hack programming languageHack programming language
Hack programming language
Radu Murzea
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
amber chaudary
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
heoff
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
eSparkBiz
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prg
alyssa-castro2326
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
Syntax
SyntaxSyntax
Syntax
Krasimir Berov (Красимир Беров)
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
Rabin BK
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
baabtra.com - No. 1 supplier of quality freshers
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
Mudasir Syed
 
perltut
perltutperltut
perltut
tutorialsruby
 
Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERL
Ravi kumar
 
Php1
Php1Php1
Php1
Keennary Pungyera
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
Dr. Ramkumar Lakshminarayanan
 

Similar to Lecture 2 php basics (1) (20)

PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
Gnugroup India
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
IIUM
 
What Is Php
What Is PhpWhat Is Php
What Is Php
AVC
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
Dave Cross
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
Khem Puthea
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
Bozhidar Boshnakov
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
PHP-Overview.ppt
PHP-Overview.pptPHP-Overview.ppt
PHP-Overview.ppt
Akshay Bhujbal
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
Marcos Rebelo
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
ajoy21
 
PHP-01-Overview.ppt
PHP-01-Overview.pptPHP-01-Overview.ppt
PHP-01-Overview.ppt
NBACriteria2SICET
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training Material
Manoj kumar
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
isadorta
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
kalaisai
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
adityathote3
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
Md. Sirajus Salayhin
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
IIUM
 
What Is Php
What Is PhpWhat Is Php
What Is Php
AVC
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
Dave Cross
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
Khem Puthea
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
ajoy21
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training Material
Manoj kumar
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
isadorta
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
kalaisai
 
Ad

Recently uploaded (20)

AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...Canadian book publishing: Insights from the latest salary survey - Tech Forum...
Canadian book publishing: Insights from the latest salary survey - Tech Forum...
BookNet Canada
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Ad

Lecture 2 php basics (1)

  • 1. PHP BasicsPrepared By: Mary Grace G. Ventura 
  • 2. PHP Scripting BlockAlways starts with <?php and ends with ?><html><head><title>Hello World Script</title></head><body><?php echo “<p>Hello World!</p>”?></body></html>
  • 3. Using Simple StatementsSimple statements are an instruction to PHP to do one simple action.There are two basic statements to output text with PHP: echo and print. Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.
  • 4. echo commandThe simplest use of echo is to print a string as argument, for example:echo “This will print in the user’s browser window.”;Or equivalentlyecho(“This will print in the user’s browser window.”);
  • 5. Echo StatementYou can also give multiple arguments to the unparenthesized version of echo, separated by commas, as in:echo “This will print in the “, “user’s browser window.”;The parenthesized version, however, will not accept multiple arguments:echo (“This will produce a “, “PARSE ERROR!”);
  • 6. Used of \n- newline echo “line 1\n”; echo “line 2”;Will producedline 1 line 2Used of <br /> echo “line 1<br />”; echo “line 2”;Will producedline 1line 2Used of <br /> and \n
  • 7. PHP Simple StatementsFollow These Rules:PHP statements end with a semicolon or the PHP ending tag. PHP doesn’t notice white space or the end of lines. It continues reading a statement until it encounters a semicolon or the PHP closing tag, no matter how many lines the statement spans.PHP statements may be written in either upper- or lowercase. In an echo statement, Echo, echo, ECHO, and eCHo are all the same to PHP.
  • 8. Comments in PHPIn PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <?php //This is a line comment # or this one /* This is a comment block */?>
  • 9. VariablesAre used for storing values such as numbers and strings so that it can be used several times in the script. “Symbolic Representation of a value”.Variables are identified and defined by prefixing their name with a dollar sign Example: $variable1, $variable2Variable names must start with a letter or underscore character (“_”)Variable names may contain letters, numbers, underscores, or dashes. There should be no spaces Variable names are CaSE- SeNSiTiVE$thisVar$ThisvAr
  • 10. Example of Variable Names$item$Item$myVariable (camel case)$this_variable$this-variable$product3$_book$__bookPage
  • 11. VariablesPHP variables are not declared explicitly instead they are declared automatically the first time they are used.It’s a loosely typed of language so we do not specify the data type of the variable.PHP automatically converts the variable to the correct data type such as string, integer or floating point numbers.
  • 12. Illustration of a Variable Declaration
  • 13. Example of Variable Variables$varName=“ace”;$$varName=1029;This is exactly equivalent to$ace=1029;
  • 14. ConstantsAlways stay the same once definedConstants are defined with the define() function. Example: define(“VARIABLE1”, “value”);Constant names follow the same rules as variable namesConstants are typically named all in UPPERCASE but do not have to be.
  • 16. ConstantsOne important difference between constants and variables is that when you refer to a constant, it does not have a dollar sign in front of it.If you want to use the value of a constant, use its name only.define(‘OILPRICE’,10);echo OILPRICE;
  • 17. Sample Program for Constant Declaration<?php define(“USER”,”Grace”); echo “Welcome ” . USER;?>Output:Welcome Grace
  • 18. IntegerAn integer is a plain-vanilla number like 75, -95, 2000,or 1.Integers can be assigned to variables, or they can be used in expressions, like so:$int_var = 12345;
  • 19. Floating PointA floating-point number is typically a fractional number such as 12.5 or 3.149391239129.Floating point numbers may be specified using either decimal or scientific notation.Ex: $temperature = 56.89;
  • 20. DoublesDoubles are floating-point numbers, such as:$first_double = 123.456;$second_double = 0.456$even_double = 2.0;Note that the fact that $even_double is a “round” number does not make it an integer. And the result of:$five = $even_double + 3;is a double, not an integer, even if it prints as 5. In almost all situations, however, you should feel free to mix doubles and integers in mathematical expressions, and let PHP sort out the typing.
  • 21. BooleanThe simplest variable type in PHP, a Boolean variable simply specifies a true or false value.TRUE=1, FALSE=0Case-InsensitiveTrue, TRUE, true are all the same.Printing out Boolean values.echo true . “\n”; //prints trueecho false; //(none)1(none)
  • 22. BooleanThe ff. are considered FALSE:Integers and floats zero(0)Empty String (“”)The string “0”Array with zero elementsNULLObject with zero member variablesEvery other value is considered TRUE.
  • 23. NULLNull is a special value that indicates no value.Case-insensitiveNULL, null, NullNULL converts to boolean FALSE and integer zero.A variable is considered to be NULL if:It has been assigned to the constant NULLIt has not been set to any value yetIt has been unset<?php$a= NULL;echo $b; ?>
  • 24. isset(), is_null()isset()Tests if a variable existsReturns FALSE if:Is set to NULLVariable has been unset()is_null()Determines if the given variable is set to NULL.Returns true if variable is NULL, FALSE otherwise.
  • 25. empty()Determines if a variable is empty.The following values are considered empty:
  • 26. isset() vs empty() vs is_null()
  • 27. StringsA string is a sequence of characters, like 'hello' or 'abracadabra'. String values may be enclosed in either double quotes ("") or single quotes ('').$name1 = ‘Ervin';$name2 = ‘Grace’;
  • 28. Singly Quoted StringsExcept for a couple of specially interpreted character sequences, singly quoted strings read in and store their characters literally.$literally = ‘My $variable will not print!\\n’;print($literally);produces the browser output:My $variable will not print!\\n
  • 29. Doubly Quoted StringsStrings that are delimited by double quotes (as in “this”) are preprocessed in both the following two ways by PHP:Certain character sequences beginning with backslash (\) are replaced with special characters.Variable names (starting with $) are replaced with string representations of their values.
  • 30. Escape Sequence Replacements Are:\n is replaced by the new line character \r is replaced by the carriage-return character\t is replaced by the tab character\$ is replaced by the dollar sign itself ($)\” is replaced by a single double-quote (“)\\ is replaced by a single backslash (\
  • 31. A Note on String Values<?php$identity = 'James Bond'; $car = 'BMW'; // this would contain the string // "James Bond drives a BMW"$sentence = "$identity drives a $car"; // this would contain the string // "$identity drives a $car"$sentence = '$identity drives a $car';?>
  • 32. A Note on String Values<?php// will cause an error due to// mismatched quotes$statement = 'It's hot outside';// will be fine$statement = 'It\'s hot outside';?>
  • 33. Data ConversionIn PHP, the type of the variable depends on the value assigned to it.
  • 34. TypecastingThere are circumstances in which you will want to control how and when individual variables are converted from one type to another.This is called typecastingTypecasting forces a variable to be evaluated as another typeThe name of the desired type is written in parentheses before the variable that is to be cast.
  • 35. Typecasting-IntegersYou can typecast any variable to an integer using the (int) operator.Floats are truncated so that only their integer portion is maintained.echo (int) 99.99; //99Booleans are cast to either one or zero(int) TRUE == 1(int) FALSE == 0Strings are converted to their integer equivalentecho (int) “test 123” ; //0echo (int) “123”;echo (int) “123test”;NULL always evaluates to zero.
  • 36. Typecasting BooleansData is cast to Boolean using the (bool) operatorecho (bool) “1”;Numeric values are always TRUE unless they evaluate to zeroStrings are always TRUE unless they are empty(bool) “FALSE” ==trueNull always evaluates to FALSE.
  • 37. Typecasting- StringsData is typecast to a string using the (string) operator:echo (string) 123;Numeric values are converted to their decimal string equivalent:(string) 123.1 == “123.1”;Booleans evaluate to either “1” (TRUE) or an empty string (FALSE)NULL values evaluates to an empty string.
  • 38. Gettype()Gets the type of a variableReturns “boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”.
  • 39. settype()Sets the type of a variable“boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”
  翻译: