2. What is PHP used for?
What is PHP?
What does PHP code look like?
Echo
Comments
Variables
Constant
3. What is PHP Used For?
PHP is a general-purpose server-side scripting
language originally designed for web development to
produce dynamic web pages
PHP can interact with MySQL databases
4. What is PHP?
PHP == ‘Hypertext Preprocessor’
Open-source, server-side scripting language
Used to generate dynamic web-pages
PHP scripts reside between reserved PHP tags
This allows the programmer to embed PHP scripts within
HTML pages
Interpreted language, scripts are parsed at run-time rather
than compiled beforehand
Executed on the server-side
Source-code not visible by client
‘View Source’ in browsers does not display the PHP code
Various built-in functions allow for fast development
Compatible with many popular databases
5. What does PHP code look like?
Structurally similar to C/C++
Supports procedural and object-oriented
paradigm (to some degree)
All PHP statements end with a semi-colon
Each PHP script must be enclosed in the reserved
PHP tag
<?php
…
?>
6. Echo
The PHP command ‘echo’ is used to output the
parameters passed to it
The typical usage for this is to send data to the
client’s web-browser
Syntax
void echo (string arg1 [, string argn...])
In practice, arguments are not passed in
parentheses since echo is a language construct
rather than an actual function
7. Echo example
Notice how echo ‘5x5=$xyz’ outputs $xyz rather than replacing it with 25
Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP
This is true for both variables and character escape-sequences (such as “n” or
“”)
<?php
$xyz = 25; // Numerical variable
$bar = “Hello”; // String variable
echo $bar; // Outputs Hello
echo $xyz,$bar; // Outputs 25Hello
echo “5x5=”,$xyz; // Outputs 5x5=25
echo “5x5=$xyz”; // Outputs 5x5=25
echo ‘5x5=$xyz’; // Outputs 5x5=$xyz
?>
8. Comments in PHP
Standard C, C++, and shell comment symbols
// for one line comment
# for one line comment
/* for multiple line.
These can span multiple lines */
9. Rules for variables:
•A variable starts with the $ sign, followed by the name of
the variable
•A variable name must start with a letter or the underscore
character($nam and $_nam)
•A variable name cannot start with a number
•A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
•Variable names are case-sensitive ($nam and $NAM are
two different variables)
Variables
10. Variables in PHP
OUTPUT is:
My Friends
2
I love My Friends
I love My Friends.
Note: 1.When you assign a text value to a
variable, put quotes around the
value.eg.”Hello Friends”
2.Here dot(.) is used for concatenation of
string)
<!DOCTYPE html>
<html>
<body>
<?php
$txt = “My Friends";
$x = 2;
echo $txt;
echo "<br>";
echo $x;
echo “I love $txt";
echo”<br>”;
echo ”I love”.$txt.”!”;
?>
</body>
</html>
11. Constant in PHP
A constant is name for a simple value. The value cannot be
changed during the script.
To create a constant, use the define() function.
Syntex
define(name, value, case-insensitive)
Parameters:
name: tell the name of the constant
value: tell the value of the constant
case-insensitive: tell whether the constant name is
case-insensitive. Default is false
12. Constant in PHP
<?php
define(“WELCOME", “to our office”,true);
echo welcome;
?>
<?php
define(“WELCOME", “to our office”);
echo WELCOME;
?>
The example below creates a constant with a case-sensitive name:
The example below creates a constant with a case-insensitive name:
NOTE:Constants are automatically global and can be used inthe entire script.
13. Differences between constants and variables
There is no need to write a dollar sign ($) before a
constant, where as in Variable one has to write a dollar
sign.
Constants cannot be defined by simple assignment,
they may only be defined using the define() function.
Constants may be defined and accessed anywhere
without regard to variable scoping rules.
Once the Constants have been set, may not be
redefined or undefined.