Php-mysql Course

PHP has a lot of built-in functions, also you can create and use your own functions.
Functions helps you to organize the various parts of a script into different tasks that must be accomplished.
To keep some PHP instructions from being executed when the page loads, you can put it into a function.

Creating your own function

You can create a function in two ways: with parameters or without parameters.
  1) The syntax for making a function without parameters is:
function function_name() {
  // Function code.
}
The name of your function must begin with either a letter or the underscore, fallowed by any combination of letters, numbers, and the underscore. You cannot use an existing function name (print, echo, isset, and so on).
  - Example:
<?php
function sayHy() {
  echo 'Hy there, how are you?';
}
?>

  2) The syntax for defining a function with parameters is:
function function_name($param1, $param2, ...) {
  // Function code.
}
The parameters $param1, $param2, etc. are variables passed into the function. These variables only matter within their function, and they don't interfere with other similar variables outside the function.
  - Example:
<?php
function sayHyTo($name) {
  echo 'Hy '. $name. ', how are you?';
}
?>

Calling Functions

Once the function is defined, you can then call and use it as you would any other function in PHP.
A function without parameters can be called using just its name fallowed by round brackets:
function_name();
A function with parameters can be called using the syntax:
function_name($arg1, $arg2, ...);
- "$arg1", "$arg2", ... are the arguments passed to the function. They are just like variables, and pass values to the function's parameters.
Notice that the function call no longer use the word "function" or curly braces.
In the place of the script where we call a function it's executed the code of that function.
  - Example:
<?php
// function without parameters
function sayHy() {
  echo 'Hy there, how are you?';
}

// function with parameters
function sayHyTo($name) {
  echo 'Hy '. $name. ', how are you?';
}

// caling the function without parameters
sayHy();                      // Hy there, how are you?

echo '<br />';

// using the function with parameters
sayHyTo('Victor');            // Hy Victor, how are you?
?>
- As you can notice, the parameters add more functionality to a function. You can use the second function ( sayHyTo() ) to say "Hy ..." to different names.
This code will output:
Hy there, how are you?
Hy Victor, how are you?

The Return Statement

A return statement is used to specify a value that a function returns when is called.
You place the return statement as the last line of the function before the closing curly bracket.
The syntax for creating a function that returns values is:
function function_name($param1, $param2, ...) {
  // Function code
  return value;
}
A function can return a value (a string or a number) or a variable whose value has been created by the function.
  - Example:
<?php
function getMed($x, $y) {
  $val = ($x + $y) / 2;
  return $val;
}

$med = getMed(8, 10);
echo $med;             // 9
?>

The return statement terminates the code execution at that point, any code within a function after the return instruction will never run.
To have a function return multiple values, use the array() function to return an array:
                return array(elm1, elm2, elm3);

Setting default parameters

Another aspect on defining your own functions is that you can give the parameter default value in the function's definition.
If a value is passed to a default parameter, the passed value is used; otherwise, the default value is used.
  - Example:
<?php
function add($x, $y=5) {
  $total = $x + $y;
  return $x. ' + '. $y. ' = '. $total;
}

// calls the add() function with one argument (for the $x parameter)
echo add(14);                           // 14 + 5 = 19

// calls the add() function with both arguments
echo '<br />'. add(14, 8);               // 14 + 8 = 22
?>
When the value for the second parameter ($y) is not pased, the function uses its default value (5).
This code will output:
14 + 5 = 19
14 + 8 = 22
You can set default values for as many of the parameters as you want. The required arguments should always be listed first, and the default parameters come last.

Recursive functions

A PHP function can be recursive, meaning it can auto-call itself.
A good way to demonstrate the ability of the recursive functions is to solve a factorial equation.
In the fallowing example we have a recursive function that finds the factorial of a number "$nr" (here 9).
<?php
function factorial($nr) {
  if ($nr>0) $re = $nr * factorial($nr-1);
  elseif ($nr==0) $re = 1;
  else $re = null;

  return $re;
}

echo '9 factorial is: '. factorial(9);        // 9 factorial is: 362880
?>
This code will output:
9 factorial is: 362880

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
PHP functions

Last accessed pages

  1. Get Mime Type of file or string content in PHP (6228)
  2. Get and Modify content of an Iframe (32346)
  3. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (59589)
  4. querySelector and querySelectorAll (30124)
  5. sPBM - Simple PHP Backup Manager (3402)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)