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

Module Week (1-5) Webapps

Uploaded by

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

Module Week (1-5) Webapps

Uploaded by

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

MODULE (Week 1 – 5)

(WEBAPPS)

INSTRUCTOR: Mr. Febrick Quiza

COURSE This course will provide students with a fundamental


DESCRIPTION: understanding of developing web-based applications.
Students will learn how to use the Laravel Framework,
applying the fundamentals of PHP to design, build, and create
effective, interactive, and dynamic web applications. Students
will use different tools to implement various web-based
applications.

COURSE After successful completion of this course, the student should


OBJECTIVES: able to:
1. Create and Design dynamic websites using Laravel
Framework
2. Applying the fundamental of PHP and
3. Implement a MySQL for its database

TOPICS: WEEK 1 – 2:
• Introduction of PHP
• PHP Install
• PHP Syntax
• PHP Comments
• PHP Variables
• PHP Echo/Print

WEEK 3:
• PHP Data Types
• PHP Strings
• PHP Numbers
• PHP Casting
• PHP Math

WEEK 4:
• PHP Constants
• PHP Magic Constants
• PHP Operators
• PHP If…Else…Elseif
• PHP Switch

WEEK 5:
• PHP Loops
• PHP Arrays
WEB APPLICATIONS DEVELOPMENT

WEEK 1 – 2:

INTRODUCTION TO PHP
What is PHP?
• PHP is an acronym for "PHP: Hypertext Preprocessor"
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server
• PHP is free to download and use

What is a PHP File?


• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code is executed on the server, and the result is returned to the browser as
plain HTML
• PHP files have extension “.php”
• 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.
• PHP is easy to learn and runs efficiently on the server side

PHP SYNTAX
• A PHP script can be placed anywhere in the document.
• A PHP script starts with “<?php” and ends with “?>”:

• The default file extension for PHP files is ".php".


• A PHP file normally contains HTML tags, and some PHP scripting code.

1|Page
WEB APPLICATIONS DEVELOPMENT

Example:
A simple .php file with both HTML code and PHP code:

PHP VARIABLES
• Variables are "containers" for storing information.
• In PHP, a variable starts with the “$” sign, followed by the name of the variable
• 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 Variables Scope:


• In PHP, variables can be declared anywhere in the script.
• The scope of a variable is the part of the script where the variable can be
referenced/used.
• PHP has three different variable scopes:
➢ local
➢ global
➢ static

2|Page
WEB APPLICATIONS DEVELOPMENT

GLOBAL AND LOCAL SCOPE

Global - a variable declared outside a function has a GLOBAL SCOPE and can
only be accessed outside a function:

Local - a variable declared within a function has a LOCAL SCOPE and can
only be accessed within that function:

PHP The “global” Keyword


• The global keyword is used to access a global variable from within a
function.
• To do this, use the global keyword before the variables (inside the
function):

3|Page
WEB APPLICATIONS DEVELOPMENT

• PHP also stores all global variables in an array called “$GLOBALS[index]”.


The index holds the name of the variable. This array is also accessible from
within functions and can be used to update global variables directly.
• The example above can be rewritten like this:

PHP The static Keyword


• Normally, when a function is completed/executed, all of its variables are
deleted. However, sometimes we want a local variable NOT to be deleted.
We need it for a further job.
• To do this, use the static keyword when you first declare the variable:

PHP “echo” AND “print” STATEMENTS


• echo and print are more or less the same. They are both used to output data to
the screen.
• The differences are small: echo has no return value while print has a return value
of “one” so it can be used in expressions. echo can take multiple parameters
(although such usage is rare) while print can take one argument. echo is
marginally faster than print.

4|Page
WEB APPLICATIONS DEVELOPMENT

The PHP “echo” Statement


• The echo statement can be used with or without parentheses: echo or
echo().
• The following example shows how to output text with the echo command
(notice that the text can contain HTML markup):

Display Text:

Display Variables:

The PHP “print" Statement


• The print statement can be used with or without parentheses: print or
print().
• The approach of echo is the same as well in print, it can output text and
variables using a print statement:

5|Page
WEB APPLICATIONS DEVELOPMENT

WEEK 3:

PHP: DATA TYPES, STRINGS, NUMBERS, CASTING &


MATH
PHP DATA TYPES
• Variables can store data of different types, and different data types can do
different things.
• PHP supports the following data types:
➢ String
➢ Integer
➢ Float (floating point numbers - also called double)
➢ Boolean
➢ Array

PHP STRINGS
• A string is a sequence of characters
• Strings in PHP are surrounded by either double quotation marks, or single
quotation marks.

Double or Single Quotes?


• You can use double or single quotes, but you should be aware of the
differences between the two.
• Double quoted strings perform action on special characters.

6|Page
WEB APPLICATIONS DEVELOPMENT

Word Count
• The PHP str_word_count() function counts the number of words in a string.

Modify Strings
• Uppercase and Lowercase

• The PHP str_replace() function replaces some characters with some other
characters in a string.

String Concatenation
• To concatenate, or combine, two strings you can use the “.” operator:

7|Page
WEB APPLICATIONS DEVELOPMENT

PHP CASTING
• Sometimes you need to change a variable from one data type into another,
and sometimes you want a variable to have a specific data type. This can be
done with casting.
• Casting in PHP is done with these statements:
➢ (string) - Converts to data type String
➢ (int) - Converts to data type Integer
➢ (float) - Converts to data type Float
➢ (bool) - Converts to data type Boolean
➢ (array) - Converts to data type Array

PHP MATH
• PHP has a set of math functions that allows you to perform mathematical tasks
on numbers.
• The pi() function returns the value of PI:

• The min() and max() functions can be used to find the lowest or highest value in
a list of arguments:

• The abs() function returns the absolute (positive) value of a number:

• The sqrt() function returns the square root of a number:

8|Page
WEB APPLICATIONS DEVELOPMENT

• The round() function rounds a floating-point number to its nearest integer:

• The rand() function generates a random number:

➢ To get more control over the random number, you can add the
optional min and max parameters to specify the lowest integer and the
highest integer to be returned.

9|Page
WEB APPLICATIONS DEVELOPMENT

WEEK 4

PHP: CONSTANTS, MAGIC CONSTANTS,


OPERATORS, IF…ELSE…ELSEIF & SWITCH

PHP CONSTANTS
• Constants are like variables, except that once they are defined, they cannot
be changed or undefined.
• A constant is an identifier (name) for a simple value. The value cannot be
changed during the script.
• A valid constant name starts with a letter or underscore (no $ sign before the
constant name).
• Note: Unlike variables, constants are automatically global across the entire
script.
• To create a constant, use the define() function.
Parameters:
➢ name: Specifies the name of the constant
➢ value: Specifies the value of the constant
➢ case-insensitive: Specifies whether the constant name should be
case-insensitive. Default is false.

Examples:

10 | P a g e
WEB APPLICATIONS DEVELOPMENT

PHP MAGIC CONSTANTS


• PHP has nine predefined constants that change value depending on where they
are used, and therefor they are called "magic constants".
• These magic constants are written with a double underscore at the start and the
end, except for the ClassName::class constant.

PHP OPERATORS
• Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
➢ Arithmetic operators
➢ Assignment operators
➢ Comparison operators
➢ Increment/Decrement operators
➢ Logical operators
➢ String operators
➢ Array operators
➢ Conditional assignment operators

11 | P a g e
WEB APPLICATIONS DEVELOPMENT

• The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.

• The basic assignment operator in PHP is "=". It means that the left operand gets
set to the value of the assignment expression on the right.

• The PHP comparison operators are used to compare two values (number or
string):

12 | P a g e
WEB APPLICATIONS DEVELOPMENT

• The PHP increment operators are used to increment a variable's value.


• The PHP decrement operators are used to decrement a variable's value.

• The PHP logical operators are used to combine conditional statements.

13 | P a g e
WEB APPLICATIONS DEVELOPMENT

• The PHP array operators are used to compare arrays.

• The PHP conditional assignment operators are used to set a value depending
on conditions:

PHP IF…ELSE…ELSEIF
In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true

14 | P a g e
WEB APPLICATIONS DEVELOPMENT

• if...else statement - executes some code if a condition is true and another code if
that condition is false

• if...elseif...else statement - executes different codes for more than two conditions

PHP SWITCH
• Use the switch statement to select one of many blocks of code to be executed.
This is how it works:
➢ The expression is evaluated once
➢ The value of the expression is compared with the values of each case
➢ If there is a match, the associated block of code is executed
➢ The break keyword breaks out of the switch block
➢ The default code block is executed if there is no match

Example:

15 | P a g e
WEB APPLICATIONS DEVELOPMENT

WEEK 5

PHP: LOOPS AND ARRAYS

PHP LOOPS
In PHP, we have the following loop types:
• while - loops through a block of code as long as the specified condition is true

Example:

Note: remember to increment $i, or else the loop will continue forever.

• do...while - loops through a block of code once, and then repeats the loop as long
as the specified condition is true
➢ The do...while loop will always execute the block of code at least once, it
will then check the condition, and repeat the loop while the specified
condition is true.

Examples:

Answer: 12345

Answer: 8

16 | P a g e
WEB APPLICATIONS DEVELOPMENT

Note: In a do...while loop the condition is tested AFTER executing the statements
within the loop. This means that the do...while loop will execute its statements at
least once, even if the condition is false. See example above.

• for - loops through a block of code a specified number of times


➢ The for loop is used when you know how many times the script should run.

This is how it works:


➢ expression1 is evaluated once
➢ expression2 is evaluated before each iteration
➢ expression3 is evaluated after each iteration

Example:

Example Explained:
1. The first expression, $x = 0;, is evaluated once and sets a counter to 0.
2. The second expression, $x <= 10;, is evaluated before each iteration, and
the code block is only executed if this expression evaluates to true. In this
example the expression is true as long as $x is less than, or equal to, 10.
3. The third expression, $x++;, is evaluated after each iteration, and in this
example, the expression increases the value of $x by one at each iteration.

• foreach - loops through a block of code for each element in an array


➢ The foreach loop - Loops through a block of code for each element in an
array of each property in an object.
➢ The most common use of the foreach loop, is to loop through the items of
an array.

Example:

17 | P a g e
WEB APPLICATIONS DEVELOPMENT

• The array above is an indexed array, where the first item has the key 0, the second
has the key 1, and so on.
• Associative arrays are different, associative arrays use named keys that you
assign to them, and when looping through associative arrays, you might want to
keep the key as well as the value.
• This can be done by specifying both the key and value in the foreach definition,
like this:

Example:

PHP ARRAYS
What is an Array?
• An array is a special variable that can hold many values under a single name,
and you can access the values by referring to an index number or name.

PHP Array Types


In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays

Working with Arrays


In these different usages that you will learn how to work with arrays, including:
• Create Arrays
• Access Arrays
• Update Arrays
• Remove Array Items
• Sort Arrays

/ * ===================== END OF THE MODULE ==================== * /

Useful link:
• https://www.w3schools.com/php/default.asp

18 | P a g e

You might also like