https://github.com/codeadamca/php-functions
A basic example of using pre-defined and user-defined PHP functions.
https://github.com/codeadamca/php-functions
functions learning-code php
Last synced: 6 months ago
JSON representation
A basic example of using pre-defined and user-defined PHP functions.
- Host: GitHub
- URL: https://github.com/codeadamca/php-functions
- Owner: codeadamca
- Created: 2020-10-08T19:02:53.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-26T21:40:44.000Z (about 1 year ago)
- Last Synced: 2025-07-11T09:32:03.321Z (8 months ago)
- Topics: functions, learning-code, php
- Language: PHP
- Homepage:
- Size: 129 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A Basic Introduction to JavaScript and Functions
A basic example of using user-defined PHP functions.
This tutorial will review how to use a series of user-defined functions for format data from a PHP array.
## The End Goal
The `functions.php` file includes an array with data for three programming books. The array closely resembles data from a MySQL database.
Similar to the arrays, if, and loops examples, your code needs to output the three values. However, this time it will use some user-defined functions to ensure the prices are formatted properly and display the rating as stars instead of a plain number.
## Steps
1. Open up a new file and name it `functions.php`.
2. Add the following code to the new PHP file:
```php
PHP Functions
PHP Functions
Use PHP echo, if statements, loops, and functions to output all three books.
array (
'name' => 'JavaScript and JQuery: Interactive Front-End Web Development',
'url' =>'https://www.chapters.indigo.ca/en-ca/books/javascript-and-jquery-interactive-front/9781118531648-item.html',
'image' => 'javascript-jquery.jpg',
'description' => 'Learn JavaScript and jQuery a nicer way.',
'rating' => 4,
'price' => '44.90',
'sale' => '39.90' ),
1 => array (
'name' => 'Php For The Web: Visual Quickstart Guide',
'url' =>'https://www.chapters.indigo.ca/en-ca/books/php-for-the-web-visual/9780134291253-item.html',
'image' => 'php-web.jpg',
'description' => 'Learn PHP programming the quick and easy way!',
'rating' => 3,
'price' => '55.99',
'sale' => '' ),
2 => array (
'name' => 'Beginning Node.js',
'url' =>'https://www.chapters.indigo.ca/en-ca/books/beginning-node-js/9781484201886-item.html',
'image' => 'beginning-nodejs.jpg',
'description' => 'A step-by-step guide to learning how to develope a Node.js application.',
'rating' => 5,
'price' => '65.90',
'sale' => '58.90' ),
);
// **************************************************
// Loop through the array and display the link information
foreach ($books as $book)
{
echo ''.$book['name'].'
';
echo '
';
}
// Use the print_r function to view the contents of the array
echo '';
print_r ($books);
echo '
';
?>
```
> [!Note]
> Do not edit the code between the stars.
3. Before the `doctype` tag create two functions. The first will accept one numeric paraemter and format it as a number using the format $.00.00. The second function will convert a rating t a series of visual start using HTML special chacaters. For example 3/5 would display ★★★☆☆.
4. In the `body` section add code that will display the three sets of book information using loops and the two functions defined in the previous step.
> [!Note]
> Add each value from the arrays one at a time. Test your PHP after each new line of PHP.
> [More information on PHP for loops](https://www.php.net/manual/en/functions.user-defined.php)
> Full tutorial URL:
> https://codeadam.ca/learning/php-functions.html
***
## Repo Resources
* [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor)
* [Filezilla](https://filezilla-project.org/) (or any FTP program)