SlideShare uma empresa Scribd logo
1 de 40
PHP Basics Prepared By: Mary Grace G. Ventura 
PHP Scripting Block Always 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 Statements Simple 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 command The simplest use of echo is to print a string as argument, for example: echo “This will print in the user’s browser window.”; Or equivalently echo(“This will print in the user’s browser window.”);
Echo Statement You 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 - newline 				echo “line 1”; 				echo “line 2”; Will produced line 1 line 2 Used of <br /> 				echo “line 1<br />”; 				echo “line 2”; Will produced line 1 line 2 Used of <br /> and
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 PHP In 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		*/ ?>
Variables Are 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, $variable2 Variable 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
Variables PHP 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;
Constants Always stay the same once defined Constants are defined with the define() function.  Example: define(“VARIABLE1”, “value”); Constant names follow the same rules as variable names Constants are typically named all in UPPERCASE but do not have to be.
Variables and Constants Ex:
Constants One 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
Integer An 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 Point A 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;
Doubles Doubles 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.
Boolean The simplest variable type in PHP, a Boolean variable simply specifies a true or false value. TRUE=1, FALSE=0 Case-Insensitive True, TRUE, true are all the same. Printing out Boolean values. echo true . “”; //prints true echo false; //(none) 1 (none)
Boolean The ff. are considered FALSE: Integers and floats zero(0) Empty String (“”) The string “0” Array with zero elements NULL Object with zero member variables Every other value is considered TRUE.
NULL Null is a special value that indicates no value. Case-insensitive NULL, null, Null NULL converts to boolean FALSE and integer zero. A variable is considered to be NULL if: It has been assigned to the constant NULL It has not been set to any value yet It has been unset <?php $a= NULL; echo $b; ?>
isset(), is_null() isset() Tests if a variable exists Returns FALSE if: Is set to NULL Variable 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()
Strings A 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 Strings Except 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 Strings Strings 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:  is replaced by the new line character   is replaced by the carriage-return character  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 (br />
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 = 'Itapos;s hot outside'; ?>
Data Conversion In PHP, the type of the variable depends on the value assigned to it.
Typecasting There are circumstances in which you will want to control how and when individual variables are converted from one type to another. This is called typecasting Typecasting forces a variable to be evaluated as another type The name of the desired type is written in parentheses before the variable that is to be cast.
Typecasting-Integers You 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; 	//99 Booleans are cast to either one or zero (int) TRUE == 1 (int) FALSE == 0 Strings are converted to their integer equivalent echo (int) “test 123” ; //0 echo (int) “123”; echo (int) “123test”; NULL always evaluates to zero.
Typecasting Booleans Data is cast to Boolean using the (bool) operator echo (bool) “1”; Numeric values are always TRUE unless they evaluate to zero Strings are always TRUE unless they are empty (bool) “FALSE” ==true Null always evaluates to FALSE.
Typecasting- Strings Data 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 variable Returns “boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”.
settype() Sets the type of a variable “boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”
Activity

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
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
 
php basics
php basicsphp basics
php basics
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Hack programming language
Hack programming languageHack programming language
Hack programming language
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
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
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
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
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Syntax
SyntaxSyntax
Syntax
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
perltut
perltutperltut
perltut
 
Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERL
 
Php1
Php1Php1
Php1
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 

Semelhante a PHP Basics: Echo, Variables, Data Types, and Typecasting

PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblockIIUM
 
What Is Php
What Is PhpWhat Is Php
What Is PhpAVC
 
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 requireTheCreativedev Blog
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning PerlDave Cross
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operatorsKhem 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 1Dave 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 .docxajoy21
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlDave Cross
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training MaterialManoj kumar
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQLkalaisai
 
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 9isadorta
 

Semelhante a PHP Basics: Echo, Variables, Data Types, and Typecasting (20)

PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
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
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
PHP-Overview.ppt
PHP-Overview.pptPHP-Overview.ppt
PHP-Overview.ppt
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
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
 
PHP-01-Overview.ppt
PHP-01-Overview.pptPHP-01-Overview.ppt
PHP-01-Overview.ppt
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
PHP Introduction and Training Material
PHP Introduction and Training MaterialPHP Introduction and Training Material
PHP Introduction and Training Material
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
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
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
 

Último

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Último (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

PHP Basics: Echo, Variables, Data Types, and Typecasting

  • 1. PHP Basics Prepared By: Mary Grace G. Ventura 
  • 2. PHP Scripting Block Always 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 Statements Simple 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 command The simplest use of echo is to print a string as argument, for example: echo “This will print in the user’s browser window.”; Or equivalently echo(“This will print in the user’s browser window.”);
  • 5. Echo Statement You 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 - newline echo “line 1”; echo “line 2”; Will produced line 1 line 2 Used of <br /> echo “line 1<br />”; echo “line 2”; Will produced line 1 line 2 Used of <br /> and
  • 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 PHP In 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. Variables Are 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, $variable2 Variable 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. Variables PHP 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. Constants Always stay the same once defined Constants are defined with the define() function. Example: define(“VARIABLE1”, “value”); Constant names follow the same rules as variable names Constants are typically named all in UPPERCASE but do not have to be.
  • 16. Constants One 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. Integer An 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 Point A 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. Doubles Doubles 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. Boolean The simplest variable type in PHP, a Boolean variable simply specifies a true or false value. TRUE=1, FALSE=0 Case-Insensitive True, TRUE, true are all the same. Printing out Boolean values. echo true . “”; //prints true echo false; //(none) 1 (none)
  • 22. Boolean The ff. are considered FALSE: Integers and floats zero(0) Empty String (“”) The string “0” Array with zero elements NULL Object with zero member variables Every other value is considered TRUE.
  • 23. NULL Null is a special value that indicates no value. Case-insensitive NULL, null, Null NULL converts to boolean FALSE and integer zero. A variable is considered to be NULL if: It has been assigned to the constant NULL It has not been set to any value yet It has been unset <?php $a= NULL; echo $b; ?>
  • 24. isset(), is_null() isset() Tests if a variable exists Returns FALSE if: Is set to NULL Variable 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. Strings A 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 Strings Except 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 Strings Strings 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: is replaced by the new line character is replaced by the carriage-return character 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 (br />
  • 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 = 'Itapos;s hot outside'; ?>
  • 33. Data Conversion In PHP, the type of the variable depends on the value assigned to it.
  • 34. Typecasting There are circumstances in which you will want to control how and when individual variables are converted from one type to another. This is called typecasting Typecasting forces a variable to be evaluated as another type The name of the desired type is written in parentheses before the variable that is to be cast.
  • 35. Typecasting-Integers You 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; //99 Booleans are cast to either one or zero (int) TRUE == 1 (int) FALSE == 0 Strings are converted to their integer equivalent echo (int) “test 123” ; //0 echo (int) “123”; echo (int) “123test”; NULL always evaluates to zero.
  • 36. Typecasting Booleans Data is cast to Boolean using the (bool) operator echo (bool) “1”; Numeric values are always TRUE unless they evaluate to zero Strings are always TRUE unless they are empty (bool) “FALSE” ==true Null always evaluates to FALSE.
  • 37. Typecasting- Strings Data 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 variable Returns “boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”.
  • 39. settype() Sets the type of a variable “boolean”, “integer”, “double”, “string”, “array”, “object”, “resource”, “NULL”