PHP: Built-in functions

This entry is part 32 of 54 in the series PHP Tutorial

PHP functions can compute and return a value.

There are some built-in functions in PHP.

The simple example below stores the return value of the built-in function number_format( ) in the variable $number.


$number = number_format(285266237);
echo "The number of records sold by Michael Jackson is about: $number";

The built-in function will format the number into 285,266,237.

PHP: Declaring functions

This entry is part 30 of 54 in the series PHP Tutorial

Function names follow the same rules as variable names:

  • they must begin with a letter or an underscore, and
  • the rest of the characters in the name can be letters, numbers, or underscores.

The 2 functions below will generate the page header html and the page footer html.


function page_header()
    print '<html><head><title>Welcome to my site</title></head>';
    print "<body bgcolor='#ffffff'>";

$user = 'Daryl';
print "Welcome, $user";

function page_footer()
    print '<hr>Thanks for visiting.';
    print '</body></html>';

PHP & OOP: Methods of a class

This entry is part 7 of 11 in the series PHP & OOP

In object-oriented programming, the member functions of a class are also called methods of the class.

If you read any literature related to object-oriented programming, you will come across this word frequently.

Look at the following example:


class PopStar
    public $person;
    public $salary;
    public $album;

    public function setSalary()
        $this->salary = 10000;

    public function getSalary()
        echo $this->salary;

$star = new PopStar();
echo "The pop star's salary is:";
echo "<br>";


There are 2 methods setSalary() and getSalary() in the above PopStar class.

From the code above, we can say that we are using the method setSalary() to set the salary of the pop star and method getSalary() to get the salary of the star.

SQL: Functions

This entry is part 20 of 20 in the series SQL Tutorial

There are a slew of mathematical functions in SQL such as avg(), max() and sum.  We have already made use of count() which will return the total number of records.

Let’s look at some common functions in SQL.

 id int auto_increment primary key, 
 artiste varchar(30), 
 album varchar(50),
 price decimal(3, 2) 

(artiste, album, price)
('Rod Stewart', 'Maggie May', 8.90 ),
('New Order', 'Substance', 7.50),
('Nirvana', 'Smell Like Teen Spirit', 9.80);

SQL Fiddle

select sum(price) from artist
select avg(price) from artist
select max(price) from artist
select min(price) from artist
select count(price) from artist

PHP: Functions

This entry is part 29 of 54 in the series PHP Tutorial

Similar to other programming languages, functions are blocks of code that together achieve a more complex task.

Functions are not executed until you call upon them.

/* Defining a PHP Function */
function writeMsg($name)
  echo "Hi, $name\n";  
  echo "How is life?";
/* Calling a PHP Function */
$john = "John";

In this example, we have passed a parameter John into the function writeMsg.

Note also that in PHP, function names are case insensitive.

If the function only returns a value, we can use the return statement.


 function sum($x,$y)
  return $z;

  echo "8 + 10 = " . sum(5,10) . "\n";
  echo "2 + 13 = " . sum(7,13) . "\n";