-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path3-functions.php
58 lines (46 loc) · 1.14 KB
/
3-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Declaring functions:
* https://www.php.net/manual/en/language.functions.php
* 1. PHP functions.
* 2. User defined functions
* 3. Scope
*
* Functions can DO things and RETURN things.
*/
// Internal function examples.
$phrase = str_replace( 'What is up?', 'Top of the morning!', 'What is up? How are you today?' );
$int_val = intval( '5' );
$position = strpos( 'My name is Nate.', 'Nate' );
echo '<pre>';
var_dump( $phrase );
var_dump( $int_val );
var_dump( $position );
var_dump( function_exists( 'strpos' ) );
// Declaring functions.
// Annonymous.
$greet = function( $name ) {
echo 'Hello ' . $name . '</br>';
};
$greet('World');
$greet('PHP');
$y = 4;
// Annonymous arrow, mind the scope.
$add = fn( $x ) => $x + $y;
// equivalent to using $y by value:
$adder = function( $x ) use ( $y ) {
return $x + $y;
};
$y = 4;
var_dump( $add(4) );
var_dump( $add(5) );
// User definded and Variable functions.
function awesome_sauce( $value ) {
echo "Take it up to $value";
return 'Or just to ' . $value;
}
echo '<pre>';
awesome_sauce( $int_val );
$awesome_sauce( $int_val );
$result = awesome_sauce( $int_val );
var_dump( $result );