0% found this document useful (0 votes)
23 views

1.PHP Basics

PHP is a server-side scripting language commonly used for web development. It allows developers to embed PHP code into HTML files and dynamically generate web pages. Some key points about PHP include: - It is platform independent and compatible with many servers like Apache and IIS. - PHP files have a .php extension and PHP code is executed on the server before sending the HTML output to the browser. - Common control structures in PHP include if/else statements, switch statements, and looping with for, while, and do-while loops.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

1.PHP Basics

PHP is a server-side scripting language commonly used for web development. It allows developers to embed PHP code into HTML files and dynamically generate web pages. Some key points about PHP include: - It is platform independent and compatible with many servers like Apache and IIS. - PHP files have a .php extension and PHP code is executed on the server before sending the HTML output to the browser. - Common control structures in PHP include if/else statements, switch statements, and looping with for, while, and do-while loops.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PHP

PHP
•Hypertext Preprocessor
•serverside scripting languages for creating dynamic Web pages
•PHP files have extension ".php“
•PHP can be easily embedded in HTML files and HTML codes can also be
written in a PHP file

Why PHP
•Platform independent(Windows, Linux, Unix, Mac OS X, etc.)
•PHP is compatible with almost all servers used today (Apache, IIS, etc.)
•PHP supports a wide range of databases including mysql
PHP
PHP SYNTAX
•A PHP script is executed on the server, and the plain HTML result is
sent back to the browser."

<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
A PHP script can be placed anywhere in the document.
PHP statements end with a semicolon (;)
PHP EXAMPLE
<!DOCTYPE html>
<html>
<body>

<h1>PHP page</h1>

<?php
echo "Hello"; ends with a semicolon

?>

</body>
</html>
PHP CASE INSENSITIVE

In PHP,
• keywords (e.g. if, else, while, echo, etc.),
• classes,
• functions
• user-defined functions are not case-sensitive.
PHP COMMENTS

// single line comment

# single line comment

/* multiple line comment */


PHP ECHO VS PRINT
ECHO PRINT
No return value Like a function ,Return value 1
Takes multiple arguments Takes one argument and can print a
single string
displays the outputs of one or more
The print outputs only the strings.
strings separated by commas.
print “Hypertext”,”Preprocessor”;
echo “Hypertext”,”Preprocessor”;
Error
Output : Hypertext Preprocessor (.) operator
used to
Faster slower concatenate
two strings
echo (or) echo() print (or) print()
echo”Hello” print “ Hello”
echo $num1."+".$num2."="; print $num1."+".$num2."=";
echo $num1 + $num2; print $num1 + $num2;
VARIABLES
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).

Rules for PHP 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
• 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 ($age and $AGE are two different variables)
• PHP is a loosely typed language, and we do not require to declare the data types of
variables, rather PHP assumes it automatically by analyzing the values.

Example : $age,$name
CONSTANTS
• Constants are either identifiers or simple names that can be assigned any
fixed values.
• They are similar to a variable except that they can never be changed
• Constants are global
•A There is no need to use dollar sign($) before constants during assignment
•A constant can only be defined using a define() function and not by any
simple assignment.

define() function syntax: •name: name of the constant.


•value: value to be stored in the
constant.
define(name, value, case_insensitive) •case_insensitive: Defines whether a
constant is case insensitive.
• default →False(case sensitive)
CONSTANTS

<?php

// This creates a case-sensitive constant

define("WELCOME", “PHP");
echo WELCOME, "\n";

// This creates a case-insensitive constant


define("WELCOME", “PHP“,true);
echo welcome;

?>
CONSTANTS
constant() function
another way to print constants using the constant() function.

Syntax
constant(name)
<?php

define("WELCOME", “PHP");

echo WELCOME, "\n";

echo constant("WELCOME");

?>
DATA TYPES

Predefined (or)

$x = null;

$x = true;

$val = 50;

stores
$arr = array( 10, 20 , 30);
$val = 50.85; reference to
functions and
class sample
resources
{}
$name = "Krishna"; external to
$obj=new sample(“obj1”);
PHP(db)
DATA TYPES
var_dump() function returns the data type and value

<?php
$modules =
array("HTML","CSS","Javascript");
var_dump($modules);
?>

array(3) { [0]=> string(4) "HTML" [1]=> string(3) "CSS" [2]=> string(10) "Javascript" }
OPERATORS
OPERATORS
Arithmetic
OPERATORS
comparison Increment/decrement Logical

-1,if $x < $y
0, if $x = $y
+1, if $x > $y
OPERATORS
string Array

Conditional assignment operator

The value of $x is expr1 if expr1 exists, and is not NULL.


If expr1 does not exist, or is NULL, the value of $x is expr2.
DECISION MAKING
<?php
if (condition){ $x = 12;
// if TRUE then execute this code if ($x > 0) {
} echo "The number is positive";
}
?>

<?php
if (condition) $x = -12;
if ($x > 0)
{
{
// if TRUE then execute this code echo "The number is positive";
} }
else{ else
{
// if FALSE then execute this code
echo "The number is negative";
} }
?>
DECISION MAKING
if (condition) <?php
{ // if TRUE then execute this code } $x = "August";
elseif if ($x == "January") {
{ // if TRUE then execute this code } echo "Happy Republic Day";
elseif }
{ // if TRUE then execute this code }
else elseif ($x == "August") {
{ // if FALSE then execute this code } echo "Happy Independence Day!!!";
}
else{
echo "Nothing to show";
}
?>
DECISION MAKING
<?php
switch(n) $n = "February";
{ case statement1: switch($n) {
code to be executed if n==statement1; case "January":
break; echo "Its January";
case statement2: break;
code to be executed if n==statement2; case "February":
break; echo "Its February";
case statement3: break;
code to be executed if n==statement3; case "March":
break; echo "Its March";
case statement4: break;
code to be executed if n==statement4; case "April":
break; ...... echo "Its April";
default: break;
code to be executed if n != any case; default:
} echo "Doesn't exist";
}
?>
LOOPING
for (initialization expression;test condition;update expression)
{ // code to be executed }

<?php

for ($num = 1; $num <= 10; $num += 2) {


echo "$num \n";
}

?>
LOOPING
while (if the condition is true)
{ // code is executed }

<?php

$num = 2;

while ($num < 12) {


$num += 2;
echo $num, "\n";
}

?>
LOOPING
do {
//code is executed
}
while (if condition is true);

<?php

$num = 2;
do {
$num += 2;
echo $num, "\n";
} while ($num < 12);

?>
LOOPING
• used to iterate over arrays
• For every counter of loop, an array element is assigned and the next counter is shifted to the next
element

foreach (array_element as value)


{ //code to be executed }

<?php
$arr = array (10, 20, 30, 40, 50, 60);
foreach ($arr as $val) {
echo "$val \n";
}
$arr = array ("Ram", "Laxman", "Sita");
foreach ($arr as $val) {
echo "$val \n";
}
?>

You might also like