Php-mysql Course

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;      // https://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;      // https://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 can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;
Calling Function and Class Method with Name from String

Last accessed pages

  1. CSS Outline (2655)
  2. Disable button and Enable it after specified time (17527)
  3. JavaScript Game - Find the Word (887)
  4. Ajax-PHP Chat Script (49473)
  5. Working with getElementsByTagName (13084)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (318)
  2. CSS cursor property - Custom Cursors (56)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (44)
  4. The Mastery of Love (41)
  5. CSS3 2D transforms (40)