Basics-03
Get to your submission folder under exercises/php-basics-01
You should create a PHP file for each step with the name of the step.
e.g : Step_1.php
COMMIT AFTER EACH STEP, call each commit by the name of the step
References
Goals:
Learn general basic programming syntax as applied to PHP
Competencies:
While loop
For loop
Functions
Variables
PHP Built in functions
While loop
Suppose you want to calculate gross salaries of 20 different persons or take a list of maximum and minimum temperatures of a certain month or a year, the while loop is ideal to solve these types of cases.
The while statement is simple, it executes the statement(s) repeatedly as long as the condition is true. The condition is checked every time at the beginning of the loop.
Flowchart
Syntax
Example
The following example print "Increment Number" with $x1 value as well as "Hello world" five times.
Code
Output
Ex 1
Write a Program to create given pattern with * using while loop
Expected Output
Ex 2
Write a program to calculate factorial of a number using while loop in php.
Input: 3 // 3 2 1 Output: 6
Input: 6 // 6 5 4 3 2 * 1 Output: 720
Ex 3
Using while loop write a programme that count number of times a word present in a sentence.
The programme should take as argument the text
and the word
to be searched for
Hint use `explode(" ", $text); // explode function looks for " " and creates an array, where each word is an element of the array
Hint use
count
function to get the number of iterations/repetitions
For
The PHP for loop allows the user to put all the loop-related statements (i.e. INITIALIZER; CONDITION; INCREMENTOR or DECREMENTOR) in one place. The structure is similar to JS language.
Flowchart
Syntax
initialize counter : Initialize the loop counter value.
test counter : Verify the loop counter whether the condition is true.
increment counter : Increasing the loop counter value.
execute the statement : Execute php statements.
Alternatively, you can write for loop statement using the following syntax also.
Example
The following example displays the numbers between 0 to 5.
Output
Counting number of times a word present in a sentence.
Output
Ex 1
Write a PHP program to compute the sum of the digits of a number.
Hint: strlen fn to get the length of a string
Hint: $word= "hello world"; $word[0] // h, $word[strlen($word) - 1]; // d
Extra Hint Click me if you get stuck
Example Input: 12345 Example Output: 15
Example Input: 9999 Example Output: 36
Ex 2 (Optional bit advanced)
Write a PHP program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.
According to Wikipedia-parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure. rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360°/4 = 90°). It can also be defined as a parallelogram containing a right angle. rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60° angle (see Polyiamond), and the latter sometimes refers specifically to a rhombus with a 45° angle.
Ex 3
Create a script to construct the following pattern, using a nested for loop.
Hint there is an repetition in the above exercise, check the middle and what is the difference between the mid upper and mid lower
Hint 2: if you are stuck check this flowchart here but remember you already made something similar in one of the above exercises
Hint 3: Don't forget to output the space and for new line use
echo PHP_EOL;
Ex 4
Write a PHP program to print alphabet pattern 'Z'. Go to the editor Expected Output:
HINT: The lucky 7 hint, the figure contains 7 lines and first and last line have 7 starts
HINT: You will be writing 2 nested loop, each have a counter that would go from 0 to 6
HINT: Don't forget to output the space and for new line use
echo PHP_EOL;
Ex 5
Using a for loop write a PHP programme to reverse a sentence. Without using built in functions
For Each
PHP 4 introduced "foreach" construct, it works only on arrays. The foreach looping is the best way to access each key/value pair from an array.
Syntax
array_expr is an array. In every loop the value of the current element of the array is assigned to $value and the internal array pointer is advanced by one and the process continue to reach the last array element.
array_expr is an array. In every loop the current element's key is assigned to $key and the internal array pointer is advanced by one and the process continue to reach the last array element.
Example -1
In the following example, we define an array with five elements and later we use foreach to access array element's value.
Output
Example -2
In the following example, both the keys and values of an array have accessed (see the second syntax of the foreach statement).
Output
Ex 1
You have an array of transactions, each of which has a debit and credit amount. Find the absolute value of the transaction amount (i.e. abs( debit - credit )) and output the transaction amount
Data
Simple Output
Ex 1
You have an array of users, each of which has a name, email and and id. On each row, Output the data available in the array
Data
Simple Output
PHP Functions
A function is a self-contained block of code that performs a specific task.
PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype()
, print_r()
, `var_dump, etc.
PHP User-Defined Functions
In addition to the built-in functions, PHP also allows you to define your own functions. It is a way to create reusable code packages that perform specific tasks and can be kept and maintained separately form main program. Here are some advantages of using functions:
Functions reduces the repetition of code within a program
Functions makes the code much easier to maintain
Functions makes it easier to eliminate the errors
Functions can be reused in other application
Creating and Invoking Functions
The basic syntax of creating a custom function can be give with:
The declaration of a user-defined
function
start with the word function, followed by the name of the function you want to create followed by parentheses i.e.()
and finally place your function's code between curly brackets{}
.Example:
Functions with Parameters
You can specify parameters when you define your function to accept input values at run time. The parameters work like placeholder variables within a function; they're replaced at run time by the values (known as argument) provided to the function at the time of invocation.
You can define as many parameters as you like. However for each parameter you specify, a corresponding argument needs to be passed to the function when it is called.
The getSum()
function in following example takes two integer values as arguments, simply add them together and then display the result in the browser.
Functions with Optional Parameters and Default Values
You can also create functions with optional parameters — just insert the parameter name, followed by an equals =
sign, followed by a default value, like this.
As you can see the third call to customFont()
doesn't include the second argument. This causes PHP engine to use the default value for the $size parameter which is 1.5.
Returning Values from a Function
A function can return a value back to the script that called the function using the return statement. The value may be of any type, including arrays and objects.
A function can not return multiple values. However, you can obtain similar results by returning an array, as demonstrated in the following example.
Understanding the Variable Scope
However, you can declare the variables anywhere in a PHP script. But, the location of the declaration determines the extent of a variable's visibility within the PHP program i.e. where the variable can be used or accessed. This accessibility is known as variable scope.
By default, variables declared within a function are local and they cannot be viewed or manipulated from outside of that function, as demonstrated in the example below:
Similarly, if you try to access or import an outside variable inside the function, you'll get an undefined variable error, as shown in the following example:
As you can see in the above examples the variable declared inside the function is not accessible from outside, likewise the variable declared outside of the function is not accessible inside of the function. This separation reduces the chances of variables within a function getting affected by the variables in the main program.
Exercises
Exercise 1
Change the following code to take the $personal_details
as a function argument, and we should have the same output as before.
Extra to read about
References
https://www.tutorialrepublic.com/php-tutorial/php-functions.php
Last updated
Was this helpful?