Download presentation
Presentation is loading. Please wait.
Published byBartholomew Cole Modified over 9 years ago
1
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL
2
Function with More than one argument and a return statement For a function to return a value, the return statement is used. Here is an example for a function that takes two arguments <?php function product($x, $y){ $result = $x * $y; return $result; } echo product(5,10).” ”; echo product(2,3); ?>
3
Arrays The array() function is used to create an array in php. Array is used when we want to store more than one value in a variable. There are three types of arrays. Indexed Associative Multi-dimensional
4
INDEXED ARRAY An array can either be index manually or automatically. Array indexing automatically starts numbering at zero. Example: supposing we want to have an array of colors. <?php $colors= array (“red”, “green”, “orange”, “blue”, “yellow”); Echo “the fifth element in the array is ”.$color[4].” ”; ?> The above array is indexed automatically. So yellow is the fifth item and is at index position 4 because numbering starts at 0.
5
The below shows how to manually index an array. <?php $colors = array(); $colors[0] = “red”; $colors[1] = “green”; $colors[2] = “orange”; $colors[3] = “blue”; $colors[4] = “yellow”; echo “the first element in the array is ”.$color[0].“ ”; ?>
6
The count() function The count() function is used to return the number of element in an array. <?php $colors= array (“red”, “green”, “orange”, “blue”, “yellow”); echo count($colors); ?> To print out all the values in an array, we could use loops. Like we did in previous lectures, we used the foreach loop, we could also use the count() function with the for loop like this
7
<?php $colors= array (“red”, “green”, “orange”, “blue”, “yellow”); $elements = count($colors); // let $i be the array index for ($i = 0; $i<$elements; $i++){ echo $colors[$i].” ”; } ?>
8
Associative arrays This is the type of array that uses named keys assigned to them. <?php $id = array(“admin”=> “001”, “user” => “002”); echo “the admin employee id is ”.$id[‘admin’].” ”; ?>
9
PHP and MySql To create database with MySQL, we are going to be using phpmyadmin. So launch wamp server and instead of going to the www directory, click on phpmyadmin or you could just open your browser and type http://localhost/phpmyadmin for the url. http://localhost/phpmyadmin Data in MySQL is stored in tables. A database is a means of storing Information. After opening phpmyadmin, click on the database tab. In the text box of create database, type a name for your database and click the create button.
10
Then, click on the newly created database to create a table. Enter the name for the table and the number of columns the table will hold. After entering a name for your column, the next thing you do is to set the type of data each column will hold by clicking on the type drop-down menu. The null check box is used to set whether a field can be left blank by the user or not. After entering all the fields, click on save. Next is to start inserting records by clicking on the insert tab. All record should be entered in the textbox of the value column. When all records have been entered, click on the go button to create the rows.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.