Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PHP class notes-unit 1, Lecture notes of Programming Languages

PHP class notes with sylabus - unit 1

Typology: Lecture notes

2018/2019

Uploaded on 08/12/2019

krishnavalli-singaravelan
krishnavalli-singaravelan 🇮🇳

5 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Table of Contents
Unit – 1..................................................................................................................................................... 1
Essenals of PHP..................................................................................................................................................... 2
Introducon..................................................................................................................................................... 2
Features..................................................................................................................................................... 2
Advantages..................................................................................................................................................... 2
Structure of a Php Program.............................................................................................................................................2
Running a php Program.................................................................................................................................................. 3
Mixing HTML and PHP.....................................................................................................................................................3
Echo Statement..................................................................................................................................................... 3
“Here” documents..................................................................................................................................................... 4
Command – Line php..................................................................................................................................................... 4
Adding COMMENTS to php code.................................................................................................................................... 5
Working with Variable.....................................................................................................................................................5
Interpolang string:..................................................................................................................................................... 6
Creang Variable Variables:............................................................................................................................................ 6
Creang Constants..................................................................................................................................................... 7
PHP Internal Data types.................................................................................................................................................. 7
Operators & Flow controls.................................................................................................................................................. 8
Operators..................................................................................................................................................... 8
Math Funcons..................................................................................................................................................... 11
Flow Control Statements...............................................................................................................................................11
Strings and Arrays..................................................................................................................................................... 17
Type Casng..................................................................................................................................................... 18
Formang Text string:.................................................................................................................................................. 18
Arrays..................................................................................................................................................... 19
Page | 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download PHP class notes-unit 1 and more Lecture notes Programming Languages in PDF only on Docsity!

Table of Contents

  • Unit – 1.....................................................................................................................................................
    • Essen�als of PHP.....................................................................................................................................................
      • Introduc�on.....................................................................................................................................................
      • Features.....................................................................................................................................................
      • Advantages.....................................................................................................................................................
      • Structure of a Php Program.............................................................................................................................................
      • Running a php Program..................................................................................................................................................
      • Mixing HTML and PHP.....................................................................................................................................................
      • Echo Statement.....................................................................................................................................................
      • “Here” documents.....................................................................................................................................................
      • Command – Line php.....................................................................................................................................................
      • Adding COMMENTS to php code....................................................................................................................................
      • Working with Variable.....................................................................................................................................................
      • Interpola�ng string:.....................................................................................................................................................
      • Crea�ng Variable Variables:............................................................................................................................................
      • Crea�ng Constants.....................................................................................................................................................
      • PHP Internal Data types..................................................................................................................................................
    • Operators & Flow controls..................................................................................................................................................
      • Operators.....................................................................................................................................................
      • Math Func�ons.....................................................................................................................................................
      • Flow Control Statements...............................................................................................................................................
    • Strings and Arrays.....................................................................................................................................................
      • Type Cas�ng.....................................................................................................................................................
      • Forma�ng Text string:..................................................................................................................................................
      • Arrays.....................................................................................................................................................

PHP Scripting Language

Unit – 1

Essentials of PHP - Operators and Flow Control - Strings and Arrays

Essentials of PHP

Introduction

• PHP stands for "PHP: Hypertext Preprocessor".

• It is a server side embedded HTML scripting language.

• its syntaxes are similar to C, Java and Perl with some unique features.

• It allow web developers to write dynamically generated pages quickly.

• PHP was created in 1994 by Rasmus Lerdorf

• Official version was released in 1995 and was known as the Personal Home Page Tools.

Features

• PHP files can contain text, HTML, CSS, JavaScript, and PHP code

• Its codes are executed on the server, and the result is returned to the browser as plain HTML.

• Files are stored with extension ".php"

• It can generate dynamic page content.

• It can create, open, read, write, and close files on the server

• It can collect form data.

• It can send and receive cookies.

• Writing a database-enabled web page is incredibly simple in PHP.

• It can encrypt data

• With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also

output any text, such as XHTML and XML.

Advantages

• PHP runs on various platforms (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

• PHP is free. Download it from the official PHP resource: www.php.net

• PHP is easy to learn and runs efficiently on the server side.

• PHP is open source software where new modules are added by the day.

Software’s required to execute PHP application:

1. To create a php script, we need to install server software.

E.g.: Microsoft IIS, Apache, Xitami, sambar server etc.,

2. Php software.

The current version used is PHP6.

3. Web browser.

Any web browser available in the system can be used.

  • Php can be inserted anywhere in the html page.
  • The php coding should be included within ‘’ tags. E.g Program 1. Using PHP and HTML together

    Using PHP and HTML together

    Here is the php information: <?php phpinfo(); ?> </ html>
  • When this page is run by the server, html part will be passed to the browser unchanged; php part will be executed by the server and its html output is sent to the browser.

Echo Statement.....................................................................................................................................................

  • To print messages, “echo” statement is used.
  • Syntax: echo “”;
  • The text to be displayed is enclosed in single or double quotes. E.g: echo “welcome to php”; echo ‘welcome to php’;
  • To pass numbers, no quotation mark is needed. E.g: echo 124.7658;
  • Passing the text within parentheses is also accepted. E.g: echo (“Welcome”);
  • To embedded html tags inside, echo statement, the following format can be used. echo “ php ”;
  • Test list of control characters in php are: \ - Displays a
    $ - Displays a $ \” - displays a ”
  • To concatenate different entities like text, number and variables in single echo statement. A commas ( ,) or dot (.) operator is used. E.g: echo “welcome” , “ to “ , “ php”; echo “Marks is”. 785; Program 1.3: <?php echo "Welcome to PHP"; echo "
    Welcome"; echo"
    "Welcome" "; echo "
    welcome"."to"."php"; echo "
    Mark is ",765; ?>

“Here” documents

  • A “here” document is just some text inserted directly in a php page, between two instances of the same token; that token is a word, such as END.
  • To display the text in a ‘here’ document, echo statement is used.

E.g: Program 1. Displaying text from PHP

Displaying text from PHP

Here's what PHP has to say:

/* This is an example Program for multi-line comment in php */ echo “hello india”; ?>

Working with Variable.....................................................................................................................................................

  • In PHP variable name begins with a $ sign.
  • Rules for naming a variable:
    • Name must start with a letter or an underscore.
    • Numbers are not allowed as first characters.
    • Keywords cannot be used as variable name. Format: $[var nm]
  • In php the variables need not be declared.
  • Whenever a variable is required, they should be assigned some value.
  • Depending on the value stored in the variable, the php automatically assigns data type.
  • The values in a variable are not static.
  • To assign data to variable, $[var nm] = value;
  • The = sign is the assignment operator in php. It allows you to assign data to variable.
  • E.g.: Program 1. <?php $a=5; echo "Value in variable a = ". $a; ?>
  • Data should be assigned some value before attempting to read a variables value.
  • To destroy a variable, unset() function is used.
  • Unset() destroys the variable and the associated data in memory.

Interpolating string:

  • In echo statement, the variables are given outside the double quotes separated by a comma. $x = 5;

E.g. echo “value in x is” , $x ;

  • String Interpolation is a feature in PHP where the name of variables can be given inside the double quotes itself.
  • In such a case as enclosing the variable in the string, echo string should be given in-between double quotes only. E.g. Program 1. <?php $a=5; echo "Value in variable a = $a"; ?> In the above program, $x is included in-between double quotes. PHP will place the value held by $x into the string like as follow:

♦ The variable in the string must be surrounded by spaces or simple punctuation.echo “value in x = 5”;

♦ E.g. Program1.

<?PHP

$type="basket"; echo "$typeball"; ?> The above echo will produce error Notice It should be altered like,: Undefined variable: typeball in C:\xampp\htdocs\prg1-9.PHP on line 3. echo “$type ball”; Where the output will be,

♦ If the output required is “basketball” without spaces, enclose the variable in curly braces like {$type}basket ball.

echo “{$type}ball”; This will return “basketball”.

♦ To display $ sign in a echo statement there are 2 ways:

1. Enclose the string in between single quotes.

E.g. echo ‘$type ball’; This will return $type ball.

2. Escape the function of $ by adding a backslash before it.

E.g. Echo “$type ball”; This would return “$type ball”.

Creating Variable Variables:

♦ Variable variables are variables that hold the name of another variable.

E.g. Program1.

  • DEG is a case sensitive constant that has the value 360.
  • $c variable will contain the product of the above 3 values.
  • o/p: 3 * DEG + pi = 1083.

    ♦ There are a number of predefined constants available in php.

    ♦ Keywords and Reserved words cannot be used for constant name.

    PHP Internal Data types..................................................................................................................................................

    ♦ In php, the data’s can be stored, without having to specify the data’s type.

    ♦ However there are data types that are used internally by PHP.

    • boolean
    • integer
    • float
    • string
    • array
    • object Holds programming objects
    • resource Holds a data resource
    • NULL Holds a value of NULL.

    ♦ When a value is assigned to a variable, PHP automatically assigns data type for that variable.

    ♦ To convert a value from one data type to another, a data “type cast” is used explicitly.

    Eg: Program 1.

    ",$i; ?>

    ♦ Php also includes special function to check the internal format of the data. They are:

    is_int (), is_float (), is_array (), and so on.

    Operators & Flow controls..................................................................................................................................................

    • Operators are used to manipulate data.
    • Flow control allows making decision in the code.

    Operators.....................................................................................................................................................

    1. Arithmetic operators:

    • These are mathematical operators that perform mathematical calculations.
    • They are + , - , * , / , % E.g: Program 1.
    $res = $op1+$op2; echo $res; $res = $op1- $op2; echo "
    ",$res; ?>

    2. Assignment operator:

    • The only assignment operators is =
    • This will assign the value in the right side to the variable in the left side.
    • E.g: $constant = 7.5;
    • (^) Multiple assignments are allowed in php.
    • E.g: $a = $b = $c = $d = 1;
    • The assignment operator can be combined with other operators called as combination assignment
    • They are += , -= ,*= , /= , %= , .= , &= , ^= , <<= , >>=.
    • E.g: .= operators is used to concatenate strings. $var = “No”; $var .= “worries”;
    • (^) Now the value in $var would be “No worries”.

    3. Increment / Decrement operators

    • Incrementing and Decrementing Operators are ++ , --.
    • For these operators, the variable should already have a value.
    • These ++ or – are executed before the rest of the statement for ++a –b.
    • When these operators are added next to a variable, the value is included after the rest of the statement is executed. E.g: Program 1.
    " .++$a; The \ symbol is a sensitive character so it should be escaped by (\\) a double slash.

    7. Comparison Operator:

    • Comparison operators are used to devise expression in a conditional clause.
    • The list of comparison operators are:

    Operator Operation Example Result == Equal $a == $b TRUE if $a is equal to $b === Identical $a === $b TRUE if $a is equal to $b, and they are of the same type. != Not equal $a != $b TRUE if $a is not equal to $b <> Not equal $a <> $b TRUE if $a is not equal to $b !== Not Identical $a !== $b TRUE if $a is not equal to $b, or they are not of the same type. < Less than $a < $b True if $a is strictly less than $b

    Greater than $a > $b True if $a is strictly greater than $b <= Less than or equal to $a <= $b True if $a is less than or equal to $b = Greater than or equal to $a >= $b True if $a is greater than or equal to $b

    E.g: Program 1.

    warning "; echo "
    your time is up"; } ?>

    8. Logical Operators:

    • Logical operators are used to combine more than one conditional clause in an expression.
    • List of logical operators in php are :

    Operator Operation Example Result and And $a and $b TRUE if both $a and $b are TRUE or Or $a or $b TRUE if both $a or $b is TRUE xor Xor $a xor $b TRUE if either $a or $b are TRUE, but not both ! Not !$a TRUE if $a is not TRUE && And $a && $b TRUE if both $a and $b are TRUE || Or $a || $b TRUE if either $a and $b is TRUE

    ‘&&’ and ‘and’ both work the same way, as is ‘||’ and ‘or’.

    • The operators &&, || have higher precedence and ‘and’ and ‘or’ have lower precedence.
    • E.g: Program 1. <?php $temp = 13; if($temp < 32 || $temp > 100) echo "Stay Inside"; else echo "temperature is warm"; ?>

    9. Ternary Operator:

    • This is a Built-in php operator that acts like a simple if statement in php. ? : These 2 operators when combined are called ternary operator. Syntax: $result = condition? Expr 1 : expr 2; E.g: Program 1. <?php $te = 66; echo ($te < 32 || $te > 100)? "Too Hot " : "Nice Day"; ?>
    • If the condition is true, the ternary operators, which is made up of the characters ?: returns expr 1. Otherwise it returns expr 2. Eg: Program 1. <?php //programming for conversion from Dec to Hex.

    4. floor(x) –To round down the number x.

    5. ceil(x) –To round up the number x.

    6. dechex(x) –To convert the given decimal number x to its equivalent hex.

    7. atan(x) –Finds the arc tangent of x.

    8. rad2deg(x) –Converts the radian value x to degree.

    9. abs(x) – Absolute Value.

    10. floor(x) – Round fraction Down.

    Eg: Program 1.

    floor(no1) = ".floor($no1); echo "
    ceil(no1) = ".ceil($no1); echo "
    decbin(floor(no1)) = ".decbin(floor($no1)); ?>

    Flow Control Statements...............................................................................................................................................

    If statement:

    • The primary decision making statement. Syntax: if (expression) { statement; } expression – php expression that evaluates to TRUE or FALSE.
    • (^) If expression is TRUE, the statement that follows is executed; if it is FALSE, statement is not executed.
    • Conditional and logical operators are used to create expressions.
    • Single if statement: $tem = 66; if($tem > 65) echo "temperature is nice.";
    • Compound if statement: $tem = 66; if($tem > 65) { echo "it "; echo "is "; echo "nice."; }
    • Compound statements are included in between curly braces { }.
    • E.g: Program 1. <?php $variable = 10.7; if (is_float($variable)) { $variable = $variable+4.5; echo $variable; var_dump($variable); } ?>
    • This example checks for the value in the variable is floating point, then adds 4.5 to it and uses the var_dump() php function to display the value in the browser.

    The else Statement:

    • The if statement checks whether the condition is true if ‘yes’ it executes the following statements.
    • If the condition is false, the else part is executed.
    • Syntax: if (expression) statements 1 else statements 2 here if the ‘expression’ is true, statement 1 is executed and if expression is false, statement 2 is executed. E.g: Program 1.
    else { echo "Too Hot"; } ?>

    • In the above example, if the condition in if statements is false. Php checks the first elseif statement’s conditional expression and if it’s true the elseif statement code is executed. If the first elseif statement’s conditional expression is false. Php moves on to the next elseif statement and so on.
    • If all elseif condition fails, the statements in else part is executed.

    Switch Statement

    • This switch is used to replace long if-elseif-else ladder of condition checking with single statement.
    • A single value is passed to a switch statement and it tests the value against the case statements list, executing the code in any case statement whose test value matches the value that is checked.

    Syntax: switch (test value) { case expr 1: stamt1; [break;] case expr 2: stmt2; [break;] case expr 3; stmt3; [break;] . . . [default: Default stmt:] }

    E.g: Program 1.

    • A value (int or float or string) is passed to the switch statement, the switch checks it against all the cases listed. If the value matches the value in the case. Then the corresponding statements are executed of no cases matches. The statements in default statement are executed.
    • The break statement ends execution of switch.

    For loop

    • To run a fixed set of statements repeatedly, a FOR loop is used. Syntax: for(expr1;expr2;expr3) { statements ; }
    • In a for loop,
      • Expr1 is executed before the loop starts.
      • Expr2, usually a condition, is checked.
      • If expr2 is true, the statements are executed.