Php-mysql Course

Generally, a function is created with the following syntax:
function function_name($parameters) {
  // function body
}
- Data from the function parameters can be of different types: number, string, array, or even object. To check the parameters data type, you can use PHP specific functions, like: is_string(), is_array(), is_object(), etc.

There is also another option for objects and arrays, by specifying in the same declaration the parameter and the allowed data type.
function function_name(ClassName $parameter) {
  // function body
}
Or, for array:
function function_name(array $parameter, array $parameter2) {
  // function body
}
- These formulas verify and ensure that the $parameter data to be an object instance of a certain class, specified in ClassName, or, at the other syntax, an Array.

  - Example:
In the next example we set a class, named AClas, a function ( fObj() ) which only accepts an argument that is an object instance of the AClas, and another function ( fArr() ) with two parameters, the first argument must be an array.
<?php
// Define AClas class
class AClas {
  public function aMet() {
    return 'Method of the AClas class';
  }
}

// this function accepts only an argument that is an object instance of the AClas
function fObj(AClas $objPar) {
  echo $objPar->aMet(). '<br />';       // calls the aMet() method
}

// the first argument passed to this function must be an Array
function fArr(array $arrPar1, $par2) {
  print_r($arrPar1);
  echo '<br />'. $par2;
}

// object instance of the AClas
$obj = new AClas();
fObj($obj);        // calls the fObj() with $obj argument

$aray = array('php', 'mysql', 'javascript');
$str = 'https://coursesweb.net';

fArr($aray, $str);         // calls the fArr() function
?>
As you can notice, the special defined parameters can be used togheter with normal parameter ( fArr(array $arrPar1, $par2) )
The code above will output:
Method of the AClas class
Array ( [0] => php [1] => mysql [2] => javascript )
http://coursesweb.net
If one of the fObj() or fArr functions is not called with the data type required in its definition, will cause an error. For example, if you call the fArr() function, and the first argument isn't an array (i.g   fArr(8, $str); ), the script will return an error like this:
Catchable fatal error: Argument 1 passed to fArr() must be an array, integer given, called in ...

Class methods with object and array arguments

If your class has methods which use object or array arguments, you can specify in the method definition the type of the allowed arguments.
  - Example:
In the following example we define two classes (AClas and BClas). The BClas has two methods: bObj() is defined to accepts for its argument only instance of the AClas, and bArr() only accepts data type Array.
<?php
// Define AClas class
class AClas {
  public function aMet() {
    return 'Method of the AClas class';
  }
}

// Define BClas class
class BClas {
  // only uses object instance of AClas
  public function bObj(AClas $obj_par) {
    return $obj_par->aMet();        // cxalls the meethod aMet() of the AClas
  }

  // only accepts an array argument
  public function bArr(array $arr_par) {
    print_r($arr_par);
  }
}

// object instance of the AClas
$objAC = new AClas();

// object instance of the BClas
$objBC = new BClas();

// calls the methods of the BClas, with the correct arguments (AClas instance for bObj(), and an array for bArr() )
echo $objBC->bObj($objAC). '<br />';
echo $objBC->bArr( array('courses', 'tutorials') );
?>
This code will output:
Method of the AClas class
Array ( [0] => courses [1] => tutorials )

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
Functions with Object and Array arguments

Last accessed pages

  1. PHP Unzipper - Extract Zip, Rar Archives (12461)
  2. Draw arrow markers between clicks coords in Canvas (2009)
  3. Countdown Timer with starting time added into a form (9439)
  4. Introduction to ActionScript 3 (2776)
  5. Writing CSS code (2521)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (522)
  2. PHP Unzipper - Extract Zip, Rar Archives (425)
  3. SHA256 Encrypt hash in JavaScript (403)
  4. Read Excel file data in PHP - PhpExcelReader (360)
  5. Create simple Website with PHP (335)