Home
 

Calling a Function Dynamically

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
  function addition ($a, $b){
      echo ($a + $b), "\n";
  }
  $result = "addition";
  $result (3,6);
?>

Output :
9

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
    function sayHello() {
      print "hello<br />";
    }
    $function_holder = "sayHello";
    $function_holder();
?>

Output :
hello

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
<?php
function addvalues ($a = 0, $b = 0){
    return $a + $b;
}
function subtractvalues ($a = 0, $b = 0){
    return $a - $b;
}
function multiplyvalues ($a = 0, $b = 0){
    return $a * $b;
}
$a = 10;
$b = 3;
$whattodo = "addvalues"; 

echo $whattodo($a, $b) . "<br />"; 

$whattodo = "subtractvalues";
echo $whattodo($a, $b) . "<br />";
?>

Output :
13
7

return the function name from a function call and calculation

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
<?php
function simple($val) {
    echo "$val"."<br>";
}
function complex($val) {
    echo "The value is " . number_format($val) . "\n";
} 

$v = 73664732;
$a = "simple";
$b = "complex";
$a($v);
$b($v);
?>

Output :
73664732
The value is 73,664,732

strtok()

strtok() function performs a similar task to explode()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$email = "l@b.ca";
$token = strtok ($email, "@");
while ($token){
    echo $token . "<br />";
    $token = strtok ("@");
}
?>

Output :
l
b.ca

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
   $info = "this:is|a,test.";

   $arr = ":|,";
   $token = strtok($info, $arr);
   while ($token) {
      echo "Node = $token<br>";
      $token = strtok($arr);
   }
?>

Output :
Node = this
Node = is
Node = a
Node = test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
$test = "index.php?id=217&sec=5&user=ajoy&title=roy";

$arr = ".?&";
$word = strtok( $test, $arr );
while ( is_string( $word ) ) {
 if ( $word ) {
   print "$word<br/>";
 }
 $word = strtok( $arr );
}
?>

Output :
index
php
id=217
sec=5
user=ajoy
title=roy

substr_replace()

1
2
3
4
5
6
7
8
9
<?php
    $id = "asdfasdf";
    $id = substr_replace( $id, "00", 3, 2);
    print "New id number: $id<br/>";
?>

Output:
New id number: asd00sdf

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$mystring = substr_replace("Hello World", "asdf", 0, 0);
echo $mystring . "<br />"; 

$mystring = substr_replace("Hello World", "0 w0", 4, 4);
echo $mystring;
?>

Output :
asdfHello World
Hell0 w0rld

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
<?php
$favs = " is good boy.";
$name = "Ram";
$favs = substr_replace($favs, $name, 0, 0);
print $favs;
?>

Output :
Ram is good boy.

trim()

This set of Javascript functions trim or remove whitespace from the ends of strings.

1
2
3
4
5
<?php
echo ">".trim(" asdf ")."<";
?>

Output :
>asdf<

1
2
3
4
5
<?php
    echo $b = trim(" testing ", " pat");
?>

Output :
esting

substr()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$str = "this is a test. this is another test"; 

if (strlen ($str) >= 30){
    echo substr ($str,0,29);
} else {
    echo $str;
}
?>

Output :
this is a test. this is anoth

1
2
3
4
5
6
7
<?php
   $arr = "2009 Lord";
   echo substr($arr, 5);
?>

Output :
Lord

Greetings based on time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
  $hour = date( "G" );
  $now = date( "g:i a" );

  $msg = "Good Evening.";
  if( $hour < 18 ) { $msg = "Good Afternoon."; }
  if( $hour < 12 ) { $msg = "Good Morning."; }

  echo( "$msg The time is $now" );
 ?>

Output:
Good Morning. The time is 10:35 am

 
 
 

Categories

Tag