0 ratings 0% found this document useful (0 votes) 20 views 24 pages PHP 2 Marks
The document provides an overview of PHP programming concepts including the foreach loop, variable creation rules, object cloning, serialization, and various functions. It also covers conditional statements, inheritance types, string functions, interfaces, and control statements like break and continue. Overall, it serves as a guide for understanding and utilizing PHP effectively.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save PHP 2 MARKS For Later O DME phon Vonpl oo anol exploc.V.
lenple he dees
» eINS ATYay ArmMenhy int) Spits aSting into on
a sinag away.
2) converth an aay in® a] convervtd a Sting inh cn
SNA Bee
coNcMe nara A WAY comets | Divid® oc Stang inf pard
: eoity A spect Fed dati mite | hosed 00 & delimiter
sgntare’
lee. ep) sig, Koni t).
: maececs(oor asices) exphote Cse -The PHP foreach Loop
0 The foreach loop works only on arrays, and is used
to loop through each key/value pair in an array.
O For every loop iteration, the value of the current
array element is assigned to $value and the array
pointer is moved by one, until it reaches the last
array element.
+ Syntax
* foreach (Sarray as $value) {
code to be executed;
}
Program for foreach
";
}
ad B locathost/minal/foreach. php
<> c @ieenexwm
wed
Breen
blue
yellowRules for creating variables in PHP:
First character of variable should — start with dollar
(S) symbol.
Second character may have an alphabet or underscore.
Third character may have alphanumeric or underscore.
Minimum two characters need for declaring php variable.
Joining of two different words of a variable cannot have
spaces between then but instead can have underscore or
camel case.
Rules for creating variables in PHP:
Syntax:
Svariable_name = assigned value;
Example:
Sa
Sname="“*Santosh”;
Sclg_name=“Gramin Polytechnic
Nanded”;
$_mobile=9000000000;In PHP, object cloning using the "clone"
keyword is used to create a complete copy
of an existing object, allowing you to
modify the copied object without
affecting the original object, essentially
providing a separate, independent instance
with the same properties at the time of
cloning; this is particularly useful when you
need to maintain the state of an original
object while making changes to a duplicate
version of it. 2In PHP, serialize() and unserialize()
are used for converting data between
different formats, typically for storing or
transmitting complex data structures such as
arrays or objects.
1. serialize()
e Purpose: Converts a PHP value (such as
an array or object) into a string
representation that can be stored ina
file, database, or sent across a network.2. unserialize()
e Purpose: Converts the serialized string
back into its original PHP value (array or
object).imagecolorallocate() Function PHP
The imagecolorallocate() function is an inbuilt function in
PHP which is used to set the color in an image. This
function returns a color which is given in RGB format.
Syntax:
int imagecolorallocate ( $image, $red, $green, $blue )
Parameters: This function accepts four parameters as
mentioned above and described below:
v Simage: It is returned by one of the image creation
functions, such as imagecreatetruecolor(). It is used to
create size of image.
By Bhojankar M. N,sort() - sort arrays in ascending order
rsort() - sort arrays in descending
order
asort() - sort associative arrays in
ascending order, according to the value
ksort() - sort associative arrays in
ascending order, according to the key
arsort() - sort associative arrays in
descending order, according to the value
krsort() - sort associative arrays in
descending order, according to the keyIn PHP, the "$" sign is used to declare and
reference variables. It precedes a variable
name to indicate that the following word is a
variable.
=> elan) el (oH
php (en) ero) oy aexele(-9
$variable = "Hello, world!";
Cleon Neb i] oNPHP is a server-side language because
php requires a server to run code. The
Code of php gets executed on the
server, and the result of execution is
returned to the browser. That's why php
is called script language and server-
side language. 16 Nov 2022PHP supports three types of inheritance:
Single inheritance
The most basic type of inheritance, with one
base class and one derived class 2
Multilevel inheritance
A parent class is inherited by a child class, which
then inherits another child class, creating a
chain of inheritance @
Hierarchical inheritance
A tree-like structure where multiple derived
classes share the same parent class @In PHP, a function is a block of code
designed to perform a specific task.
Functions allow for code reusability,
modularity, and organization, enabling
developers to write cleaner and more
maintainable code.
Types of Functions in PHP:
‘(lo
Bo
eT ese neue l tty
OES Te Pl osilatve MU alot (ol ats}
Anonymous Functions (Closures)
Arrow Functions
Recursive Functions
Variable Functic yoOoooonooooono
Advantages of PHP
Open Source
Platform Independent
Simple & easy
Database
Fast
Support
Security
Maintenance
Fast Performance
Vast community
Scripting LanguageConditional Statements
In PHP we have the following conditional statements:
O if statement - executes some code if one condition is
true
O if...else statement - executes some code if a
condition is true and another code if that condition is
false
O if...elseif...else statement - executes different codes
for more than two conditions
0 switch statement - selects one of many blocks of
code to be executed
if statement
The if statement executes some code if one condition is
true
Syntax
if (condition) {
code to be executed if condition is true;
}
Ex. <2php locos minalitghp
$t = 20;
if ($t>0) { € > @ Olea
echo "Number is positive"; Number is positive
}
>if...elseif...else Statement
The if...elseif...else statement executes different codes
for more than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
elseif (condition) {
code to be executed if first condition is false and this
condition is true;
else {
code to be executed if all conditions are false;
}
By: Bhojankar M,N.nested if Statement
The nested if statement contains the if block inside
another if block. The inner if statement executes only
when specified condition in outer if statement is true.
Syntax
if (condition) {
//code to be executed if condition is true
if (condition) {
//code to be executed if condition is true
}switch Statement
Use the switch statement to select one of many blocks of code to
be executed.
Syntax
switch (n) {
case labell:
code to be executed if n=labell;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
default:
code to be executed if n is different from all labels;
By: Bhojankar M,N.Half_Odd round of 9.5 is : 9
String Function in PHP
ane evens a sing ets)
supe) Reruns the poston of the fiat stpitingsen)
other sing eases)
sreplicc)——Rephcssone ances ina sting steplassing 16 be
(cosesive) repos tes)
ewer Comer the Fist chance of ech sonore)
swaps) Canes ig up ttn)
String Function in PHP:
Pasction Dosrgtion synta
sepa) Repeating sing wih 8 specie sr repeaing pe)
060) er)
Iti finson res he 80
bret
sua) btn) fiction wd t play of sb ting tng)ay
Function
sig)
ar,
ston
© 21 of 27. tewtom rs ose 0
Sings Sees ET
areca
‘rosie ambos the to eng
subs) fncton welt dpa or st sings)
String Function in PHP
Tocamenastingtoanaray sr splitting ng)
‘ng
Renovesthe white sues from end of ining S30)
‘tev
Removes the white spses foe I img Se)
Remove weyace or athe chopsting cas
‘elcined Charro the neh
Sndot ating
String Function in PHP.pul
implement, without involving the complexities and details
of how the particular methods are implemented.
0 An Interface is defined just like a class but with the class
keyword replaced by the interface keyword and just the
function prototypes.
The interface contains no data variables.
An interface consists of methods that have no
implementations, which means the interface methods are
abstract methods.
oa
By: Bhojnkar MLN
ype. .
0 Interfaces are different ont HOSEEeserneeeneen
inherit from one class only whereas the class can
implement one or more interfaces.
1 Interface enable to model multiple inheritance.Class child_class_ name:
implements interface_namel,.....
Syntax2:
interface(using multiple interface)
Class child_class_name implements interface_namel,
interface name2,
By: Bhajnkar M,N
‘
bic ds
im Con Aime
fcton dap)
hoe
bc tn gt
$s) new cr
somo,
Soc,
Ss;Break Statement
0 HP break statement breaks the execution of the current for,
while, do-while, switch, and for-each loop.
O Ifyou use break inside inner loop, it breaks the execution of
inner loop only.
0 The break keyword immediately ends the execution of the
loop or switch structure.
O It breaks the current flow of the program at the specified
condition and program control resumes at the next
statements outside the loop.
0 The break statement can be used in all types of loops such as
while, do-while, for, foreach loop, and also with switch case.
Syntax
jump statement;
breal
Program
<2php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x ";
} Wiican
- <> c @
The number is:
The number is
The number is
The number is
wNmoContinue Statement
The continue statement breaks one iteration (in the
loop), if a specified condition occurs, and
continues with the next iteration in the loop.
Ex. "; The number is: 5
The number is: 6
} The number is: 7
s The number is: 8
. The number is: 9