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.

<?php

$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.

<?php

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

page_header();
$user = 'Daryl';
print "Welcome, $user";
page_footer();

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:

<?php

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

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

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

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

?>

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.

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

INSERT INTO artist
(artiste, album, price)
VALUES
('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.

<?php
/* Defining a PHP Function */
function writeMsg($name)
{
  echo "Hi, $name\n";  
  echo "How is life?";
}
/* Calling a PHP Function */
$john = "John";
writeMsg($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.

<?php

 function sum($x,$y)
 {
  $z=$x+$y;
  return $z;
 }

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

?>