Connection failed: could not find driver

PHP development


Lesson 9

PHP functions

PHP Built-in Functions

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

PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.
Check this reference:W3 schools PHP reference

PHP User Defined Functions

Besides the built-in PHP functions, it is possible to create your own functions.

Create a function


function myFunction($myArgument)
{
    echo $myArgument;
}

Call a function


myFunction('hello World');

since function names are NOT case sensitive you can also call it with:


myfunction('hello World');

Default arguments


function mySecondFunction($myArgument, $mySecondArgument = 'world')
{
    echo $myArgument . ' ' . $mySecondArgument;
}

mySecondFunction('Hello'); // will print 'Hello world' mySecondFunction('Hello', 'Jake'); // will print 'Hello Jake'

Function return

A function can return a value (int, string, boolean, array, object).


function myThirdFunction($myArgument)
{
    return $myArgument;
}

myThirdFunction('Hello World!'); //nothing will be printed echo myThirdFunction('Hello World!'); // Hello World! will be printed
$var = myThirdFunction('Hello World!'); echo $var;

$var2 = ['hello', 'banana', 'world']; $var3 = myThirdFunction($var2); echo $var3; //what will be printed?

Passing arguments by reference

by adding a & in front of the argument you tell the function that it is passed by reference. In doing so you are able to change the value of a variable with such a function.


function addSomething(&$number, $addition){
{
    $number += $addition;
}

$myNumber = 3;
addSomething($myNumber,6);
echo myNumber; //prints 9

  

Variadic function

With a Variadic function you can pass a variable number of arguments to the function by adding ... in front of the argument like ...$myArgument.

NOTE that this can only be done as the last argument.


function myVariadic(...$var)
{
    foreach($var as $value){
        echo $value . '<br />';
    }
}

myVariadic(1,3,5,7,9,'hello','world');

De above function prints:
1
3
5
7
9
hello
world

Data tye of arguments

PHP is a loosley typed language meaning that we do not have to declare what type a variable or argument is. example:


$var = 6;    //integer
$var2 = '9'; //string

echo $var + $var2; // prints 15
$var2 = '5 days';
echo $var + $var2; // prints 11

this can cause unexpected behaviour of your program. For that reason we can declare the variable type for function arguments.
By declaring strict types as true it will generate an error when we pass the wrong type to the function.


<?php 
declare(strict_types = true);

function myDeclaredArguments(int $a, int $b)
{
    echo $a + $b;
}

myDeclaredArguments(2, 5);     // prints 7
myDeclaredArguments(2, '5');   // prints error

Return types

for the same reason we can also declare the return type for a function.
If the returned value is not the same as the declared return type it will generate an error when strict_types == true.


<?php 
declare(strict_types = true);

function myDeclaredReturnTypeFunction(int $a, int $b) : int
{
    if ( $a == $b ) 
    {
        return $a + $b;
    } else {
        return false;
    }
}

var_dump(myDeclaredReturnTypeFunction(5, 5));     // prints integer(10)
var_dump(myDeclaredReturnTypeFunction(2, 5));     // prints error not allowed to return a boolean

Homework

Create a function that calculates the temperature to Celcius or Fahrenheit. It takes 2 arguments, a float and a string.
- The value of string MUST be 'FtoC' of 'CtoF'. If it is a different value an error should be printed.
- The return value is an array that incorparates the temperature value and the temperature type, see example below.
- Bare in mind that if another programmer should look at your code it should be obvious what you are trying to accomplish. So choose the name of your function and arguments well

with this homework assignment you are supposed to find out how you calculate this mathematically and turn that into a php function.


Example:

$returnvalue = ['temperature' => 20.1, 'type' => 'Celsius' ];