Thursday, October 15, 2020

PHP Short note part 2 (Conditional statements, Loops, Functions, Arrays)

This is a quick read on Conditional statements, Loops, Functions, and Arrays. I've created this short note to help anyone who wants to refresh their PHP knowledge. And for people who are new to PHP but have some knowledge in programming can also benefit from this article.

Conditional Statements

This is used to make choices based on the conditions given.





Switch

Compare the same expression with many different values and depending on the value, a different code is executed.


PHP Loops






Break and Continue statements

Break – To jump out of a loop or a switch statement

Continue – If a specific condition occurs skip one iteration and continue with the next 

PHP Functions

PHP has 2 types of functions. 

    1. PHP built-in Functions which can be called directly 

    2. PHP user-defined functions 

User-defined functions

This is a block of code that can be used over repeatedly when you call it. It doe not run automatically.

Function functionName(Arguments1 ){

Code;

}

functionName( Arguments1); //Calling the function

function name should start with aletter or an underscore and it is not case sensitive.

When calling a function an argument can be passed to it as well. There’s an option to declare a default argument too.

Functions can return values with return statement as well.


Static Declaration

PHP is a loosely typed language and it automatically assigns data typed to variables according to their values. That is why the following code works without an error.


The static declaration can be used to specify an expected data type for a function. It can also be used to specify a return data type as well.

First of all, you should add the following statement as the first line pf the PHP code.

<?php declare(static_types=1);

                Function functionName(int Arg1, int Arg2)

?>

To declare a type after return statement use colon after the function arguments;

                Function functionName(int Arg1, int Arg2):float{

}




Passing arguments by reference

The arguments that are passed to a function do not change the original when it is used in the function. But to change the original value of a variable you can use & operator.


Arrays

An array is a list of items stored in a variable.

To create an array – array();




With concluding arrays the basics of PHP comes to an end. And in the next article, we will discuss Forms and POST/GET methods.

 References

https://www.w3schools.com/php/default.asp

Authors Note

It's been a while since I last coded in PHP. But I always liked the language. w3schools has been the main site where I learned my basics and I created this short note referring to that site. If you need a more elaborate explanation it is the place to go. This article is mainly written as a quick read to refresh the memory.



No comments:

Post a Comment

Review of California Consumer Privacy Act (CCPA) and the amendment of California Privacy Rights Act (CPRA)

California Consumer Privacy Act (CCPA) became effective on 1st of January 2020 enhancing the privacy rights and consumer protection for the ...