PHP: Built-in functions

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: Passing arguments to functions

The input values supplied to a function are called arguments.

We can modify page_header( ) to take an argument that holds the page color.

<?php

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

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

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

 

Posted in

PHP: Declaring functions

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>';
}

MySQL Clients: Creating DB and table

Using an earlier example, let’s go ahead and create a database and a table.

We will use the same code illustrated in the example.

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);

First create the database using

create database allartist

console_create

Now we create a table and insert a row into it.

We need to use use allartist to ensure that the table is created under the table allartist.

console_createtable

console_insert

We can use select * from artist to check if the table has the inserted data.

MySQL Clients: Dropping DB and table

Now that we know that the database allartist and a table known as artist have been created.  Let’s drop them and recreate them.

Since there is a table known as artist that is attached to the database allartist, we would have to first drop table artist.

To drop the table, execute the command:

use allartist
drop table artist

Next we drop the database using the command:

drop database allartist

console_drop