Here is the easiest way to call a Function, or Class method dinamically, with Name from a String, stored into a variable or array item.
- Click on the code to select it.

1. Calling function with name stored into a variable use: $variable($parameters)
<?php
function url($adr){
  return 'http://'. $adr;
}

// variable with function name
$fun = 'url';

// calls the functions with name from $fun
$val = $fun('coursesweb.net/php-mysql/');

echo $val;      // http://coursesweb.net/php-mysql/

2. Calling function with name stored into a string in array item use: $array["key"]($parameters)
<?php
function getSum($x, $y){
  return $x + $y;
}

function hi(){
  echo 'Hello Friend';
}

// array with function names
$arr = ['f1'=>'getSum', 'f2'=>'hi'];

// calls the functions with name from $arr array
$sum = $arr['f1'](12, 89);

echo $sum;      // 101
echo $arr['f2']();      // Hello Friend

3. Calling class method with name from string in variable (add the variable with the method name within " {} "):
$object->{$variable}($parameter)
<?php
// PHP Class
class Cls {
  // class method
  public function site($str){
    return 'http://'. $str;
  }
}

// varible with method name
$method = 'site';

$obC = new Cls;
$val = $obC->{$method}('coursesweb.net/');

echo $val;      // http://coursesweb.net/

4. Calling class method with name from string in array item (add the array item with the method name within " {} "):
$object->{$array['key']}($parameters)
<?php
// PHP Class
class Cls {
  // class methods
  public function getSum($x, $y){
    return $x + $y;
  }

  public function hi(){
    return 'Hello Friend';
  }
}

// array with method names
$mts = ['m1'=>'getSum', 'm2'=>'hi'];

$obC = new Cls;
$sum = $obC->{$mts['m1']}(12, 89);
$msg = $obC->{$mts['m2']}();

echo $sum;      // 101
echo $msg;      // Hello Friend

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Calling Function and Class Method with Name from String

Last accessed pages

  1. Gravitee (406)
  2. Get the difference between two Dates - Time and Date (905)
  3. Read Excel file data in PHP - PhpExcelReader (49328)
  4. Polygons with CSS (6194)
  5. PHP MySQL - WHERE and LIKE (15455)

Popular pages this month

  1. Qwop (5062)
  2. Drag Racer V3 (2444)
  3. Flash Games - Free online Games (1201)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1065)
  5. Bubbles3 (1023)