├── .gitattributes
├── 9781484271407.jpg
├── Contributing.md
├── LICENSE.txt
├── README.md
├── ch01
└── copyright.php
├── ch02
├── phpinfo.php
└── phpversion.php
├── ch03
├── copyright.php
└── error.php
├── ch04
├── casting.php
├── data_conversion_01.php
├── data_conversion_02.php
├── data_tests.php
├── dowhile.php
├── foreach_01.php
├── foreach_02.php
├── forloop.php
├── functions_01.php
├── functions_02.php
├── functions_03.php
├── functions_04.php
├── functions_05.php
├── functions_06.php
├── functions_07.php
├── functions_08.php
├── functions_09.php
├── functions_10.php
├── generator.php
├── heredoc.php
├── inspect_array.php
├── phptest.php
├── quotes_01.php
├── quotes_02.php
├── quotes_03.php
├── quotes_04.php
├── unpack.php
├── variable_variables.php
└── while.php
├── ch05
├── blog.php
├── blog_02.php
├── blog_03.php
├── contact.php
├── contact_02.php
├── contact_03.php
├── details.php
├── document_root.php
├── error.php
├── footer_01.php
├── footer_02.php
├── gallery.php
├── gallery_02.php
├── gallery_03.php
├── get_filename.php
├── index_01.php
├── index_02.php
├── index_03.php
├── index_04.php
├── index_05.php
├── index_06.php
├── index_07.php
├── menu_01.php
├── menu_02.php
├── random_image_01.php
├── random_image_02.php
└── title.php
├── ch06
├── bad_link.php
├── bad_link_end.php
├── blog.php
├── contact_01.php
├── contact_02.php
├── contact_03.php
├── contact_04.php
├── contact_05.php
├── contact_06.php
├── contact_07.php
├── contact_08.php
├── contact_09.php
├── details.php
├── error.php
├── form.php
├── form_end.php
├── gallery.php
├── includes
│ ├── footer.php
│ ├── menu.php
│ ├── processmail_01.php
│ ├── processmail_02.php
│ ├── processmail_03.php
│ ├── processmail_04.php
│ ├── processmail_05.php
│ ├── random_image.php
│ └── title.php
├── index.php
├── missing.php
└── thank_you.php
├── ch07
├── buildlist.php
├── download.php
├── fopen_append.php
├── fopen_exclusive.php
├── fopen_read.php
├── fopen_readloop.php
├── fopen_write.php
├── get_contents_01.php
├── get_contents_02.php
├── get_contents_03.php
├── get_contents_04.php
├── get_contents_05.php
├── get_contents_06.php
├── getcsv.php
├── getdownloads.php
├── imagelist_01.php
├── imagelist_02.php
├── iterator_01.php
├── iterator_02.php
├── iterator_03.php
├── iterator_04.php
├── iterator_05.php
├── newsfeed.php
├── scandir.php
├── sonnet.txt
└── weather.csv
├── ch08
├── ListBuilder_end.php
├── array_change_key_case_01.php
├── array_change_key_case_02.php
├── array_combine.php
├── array_diff.php
├── array_diff_assoc.php
├── array_diff_key.php
├── array_diff_uassoc.php
├── array_diff_ukey.php
├── array_intersect.php
├── array_intersect_assoc.php
├── array_intersect_key.php
├── array_intersect_uassoc.php
├── array_intersect_ukey.php
├── array_map_01.php
├── array_map_02.php
├── array_map_03.php
├── array_map_04.php
├── array_walk_01.php
├── array_walk_02.php
├── array_walk_03.php
├── array_walk_04.php
├── commas_01.php
├── commas_02.php
├── csv_list_01.php
├── csv_list_02.php
├── csv_processor.php
├── csv_splat_01.php
├── csv_splat_02.php
├── data
│ ├── film_locations.json
│ ├── scores.csv
│ └── weather.csv
├── json_01.php
├── json_02.php
├── json_03.php
├── json_04.php
├── list_01.php
├── list_02.php
├── list_03.php
├── list_04.php
├── merge_01.php
├── merge_02.php
├── merge_03.php
├── merge_04.php
├── merge_05.php
├── merge_06.php
├── merge_07.php
├── modify_01.php
├── modify_02.php
├── modify_03.php
├── modify_04.php
├── modify_05.php
├── multidimensional_01.php
├── multidimensional_02.php
├── multisort_01.php
├── multisort_02.php
├── multisort_03.php
├── multisort_04.php
├── multisort_05.php
├── natsort.php
├── permutations.php
├── spaceship_01.php
├── spaceship_02.php
├── unique_01.php
└── unique_02.php
├── ch09
├── Php8Solutions
│ └── File
│ │ ├── Upload.php
│ │ ├── Upload_01.php
│ │ ├── Upload_02.php
│ │ ├── Upload_03.php
│ │ ├── Upload_04.php
│ │ └── Upload_05.php
├── file_upload_01.php
├── file_upload_02.php
├── file_upload_03.php
├── file_upload_04.php
├── file_upload_05.php
├── file_upload_06.php
└── multi_upload.php
├── ch10
├── Php8Solutions
│ └── Image
│ │ ├── Thumbnail.php
│ │ ├── ThumbnailUpload.php
│ │ ├── Thumbnail_01.php
│ │ ├── Thumbnail_02.php
│ │ └── Thumbnail_03.php
├── create_thumb_01.php
├── create_thumb_02.php
├── create_thumb_03.php
├── create_thumb_upload_01.php
└── create_thumb_upload_02.php
├── ch11
├── Php8Solutions
│ └── Authenticate
│ │ ├── CheckPassword.php
│ │ ├── CheckPassword_01.php
│ │ └── CheckPassword_02.php
├── authenticate.php
├── authenticate_01.php
├── authenticate_02.php
├── login_01.php
├── login_02.php
├── login_03.php
├── logout.php
├── menu_01.php
├── menu_02.php
├── menu_03.php
├── menu_04.php
├── menu_05.php
├── multiform.php
├── multiple_01.php
├── multiple_01_done.php
├── multiple_02.php
├── multiple_02_done.php
├── multiple_03.php
├── multiple_03_done.php
├── multiple_04.php
├── multiple_04_done.php
├── register_01.php
├── register_02.php
├── register_03.php
├── register_04.php
├── register_05.php
├── register_user_csv_01.php
├── register_user_csv_02.php
├── secretpage_01.php
├── secretpage_02.php
├── secretpage_03.php
├── secretpage_04.php
├── session_01.php
├── session_02.php
├── session_03.php
├── session_04.php
└── session_timeout.php
├── ch12
└── images.sql
├── ch13
├── connection.php
├── connection_test.php
├── mysqli_01.php
├── mysqli_02.php
├── mysqli_03.php
├── mysqli_04.php
├── mysqli_05.php
├── mysqli_06.php
├── mysqli_07.php
├── mysqli_08.php
├── mysqli_09.php
├── mysqli_10.php
├── mysqli_integer_01.php
├── mysqli_integer_02.php
├── mysqli_integer_03.php
├── mysqli_order_01.php
├── mysqli_order_02.php
├── mysqli_prepared_01.php
├── mysqli_prepared_02.php
├── pdo_01.php
├── pdo_02.php
├── pdo_03.php
├── pdo_04.php
├── pdo_05.php
├── pdo_06.php
├── pdo_07.php
├── pdo_08.php
├── pdo_09.php
├── pdo_10.php
├── pdo_integer_01.php
├── pdo_integer_02.php
├── pdo_integer_03.php
├── pdo_order_01.php
├── pdo_order_02.php
├── pdo_prepared_01.php
├── pdo_prepared_02.php
├── pdo_prepared_03.php
└── utility_funcs.php
├── ch14
├── connection.php
├── footer.php
├── gallery_01.php
├── gallery_mysqli_02.php
├── gallery_mysqli_03.php
├── gallery_mysqli_04.php
├── gallery_mysqli_05.php
├── gallery_mysqli_06.php
├── gallery_mysqli_07.php
├── gallery_mysqli_08.php
├── gallery_pdo_02.php
├── gallery_pdo_03.php
├── gallery_pdo_04.php
├── gallery_pdo_05.php
├── gallery_pdo_06.php
├── gallery_pdo_07.php
├── gallery_pdo_08.php
├── menu.php
└── title.php
├── ch15
├── blog.sql
├── blog_delete_mysqli.php
├── blog_delete_pdo.php
├── blog_insert_mysqli_01.php
├── blog_insert_mysqli_02.php
├── blog_insert_pdo_01.php
├── blog_insert_pdo_02.php
├── blog_list_mysqli_01.php
├── blog_list_mysqli_02.php
├── blog_list_mysqli_03.php
├── blog_list_norec_mysqli.php
├── blog_list_norec_pdo.php
├── blog_list_pdo_01.php
├── blog_list_pdo_02.php
├── blog_list_pdo_03.php
├── blog_update_mysqli_01.php
├── blog_update_mysqli_02.php
├── blog_update_mysqli_03.php
├── blog_update_pdo_01.php
├── blog_update_pdo_02.php
├── blog_update_pdo_03.php
└── connection.php
├── ch16
├── blog_01.php
├── blog_left_mysqli.php
├── blog_left_pdo.php
├── blog_limit_mysqli.php
├── blog_limit_pdo.php
├── blog_list_mysqli.php
├── blog_list_pdo.php
├── blog_mysqli.php
├── blog_para_mysqli.php
├── blog_para_pdo.php
├── blog_pdo.php
├── blog_ptags_mysqli.php
├── blog_ptags_pdo.php
├── blog_word_mysqli.php
├── blog_word_pdo.php
├── connection.php
├── date_converter.php
├── date_converter_01.php
├── date_converter_02.php
├── date_format_01.php
├── date_format_02.php
├── date_format_03.php
├── date_format_04.php
├── date_format_05.php
├── date_interval_01.php
├── date_interval_02.php
├── date_interval_03.php
├── date_interval_04.php
├── date_interval_05.php
├── date_interval_06.php
├── date_modify_01.php
├── date_modify_02.php
├── date_test.php
├── footer.php
├── menu.php
├── setDate.php
├── timezone_display.php
├── timezones.php
├── title.php
└── utility_funcs.php
├── ch17
├── blog_update_mysqli_04.php
├── blog_update_pdo_04.php
├── connection.php
├── details_01.php
├── details_mysqli_01.php
├── details_mysqli_02.php
├── details_mysqli_03.php
├── details_pdo_01.php
├── details_pdo_02.php
├── details_pdo_03.php
├── footer.php
├── menu.php
└── test.php
├── ch18
├── Php8Solutions
│ └── File
│ │ ├── Upload.php
│ │ └── Upload_01.php
├── blog_delete_mysqli_innodb.php
├── blog_delete_mysqli_myisam_cascade.php
├── blog_delete_pdo_innodb.php
├── blog_delete_pdo_myisam_cascade.php
├── blog_insert_mysqli_01.php
├── blog_insert_mysqli_02.php
├── blog_insert_mysqli_03.php
├── blog_insert_mysqli_04.php
├── blog_insert_mysqli_05.php
├── blog_insert_pdo_01.php
├── blog_insert_pdo_02.php
├── blog_insert_pdo_03.php
├── blog_insert_pdo_04.php
├── blog_insert_pdo_05.php
├── blog_list_mysqli_04.php
├── blog_list_mysqli_05.php
├── blog_list_pdo_04.php
├── blog_list_pdo_05.php
├── blog_update_mysqli_04.php
├── blog_update_mysqli_05.php
├── blog_update_mysqli_06.php
├── blog_update_mysqli_07.php
├── blog_update_pdo_04.php
├── blog_update_pdo_05.php
├── blog_update_pdo_06.php
├── blog_update_pdo_07.php
├── categories.sql
├── image_delete_mysqli_myisam_null.php
├── image_delete_mysqli_myisam_restrict.php
├── image_delete_pdo_myisam_null.php
├── image_delete_pdo_myisam_restrict.php
├── storage_engines.php
└── toggle_fields.js
├── ch19
├── Php8Solutions
│ └── Authenticate
│ │ └── CheckPassword.php
├── authenticate_2way_mysqli.php
├── authenticate_2way_pdo.php
├── authenticate_mysqli.php
├── authenticate_pdo.php
├── login_db.php
├── login_mysqli.php
├── login_pdo.php
├── logout_db.php
├── menu_db.php
├── register_2way_mysqli.php
├── register_2way_pdo.php
├── register_db.php
├── register_db_mysqli.php
├── register_db_pdo.php
├── register_user_csv.php
├── register_user_mysqli.php
├── register_user_pdo.php
├── secretpage_db.php
└── session_timeout_db.php
├── errata.md
├── images
├── basin.jpg
├── fountains.jpg
├── fuji.jpg
├── kinkakuji.jpg
├── maiko.jpg
├── maiko_phone.jpg
├── menu.jpg
├── monk.jpg
├── ryoanji.jpg
└── thumbs
│ ├── basin.jpg
│ ├── fountains.jpg
│ ├── kinkakuji.jpg
│ ├── maiko.jpg
│ ├── maiko_phone.jpg
│ ├── menu.jpg
│ ├── monk.jpg
│ └── ryoanji.jpg
└── styles
├── admin.css
├── journey.css
└── newsfeed.css
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/9781484271407.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/9781484271407.jpg
--------------------------------------------------------------------------------
/Contributing.md:
--------------------------------------------------------------------------------
1 | # Contributing to Apress Source Code
2 |
3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers.
4 |
5 | ## How to Contribute
6 |
7 | 1. Make sure you have a GitHub account.
8 | 2. Fork the repository for the relevant book.
9 | 3. Create a new branch on which to make your change, e.g.
10 | `git checkout -b my_code_contribution`
11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
12 | 5. Submit a pull request.
13 |
14 | Thank you for your contribution!
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Freeware License, some rights reserved
2 |
3 | Copyright (c) 2022 David Powers
4 |
5 | Permission is hereby granted, free of charge, to anyone obtaining a copy
6 | of this software and associated documentation files (the "Software"),
7 | to work with the Software within the limits of freeware distribution and fair use.
8 | This includes the rights to use, copy, and modify the Software for personal use.
9 | Users are also allowed and encouraged to submit corrections and modifications
10 | to the Software for the benefit of other users.
11 |
12 | It is not allowed to reuse, modify, or redistribute the Software for
13 | commercial use in any way, or for a user’s educational materials such as books
14 | or blog articles without prior permission from the copyright holder.
15 |
16 | The above copyright notice and this permission notice need to be included
17 | in all copies or substantial portions of the software.
18 |
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | SOFTWARE.
26 |
27 |
28 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Apress Source Code
2 |
3 | This repository accompanies [*PHP 8 Solutions*](https://www.apress.com/9781484271407) by David Powers (Apress, 2022).
4 |
5 | [comment]: #cover
6 | 
7 |
8 | Download the files as a zip using the green button, or clone the repository to your machine using Git.
9 |
10 | ## Releases
11 |
12 | Release v1.0 corresponds to the code in the published book, without corrections or updates.
13 |
14 | ## Contributions
15 |
16 | See the file Contributing.md for more information on how you can contribute to this repository.
--------------------------------------------------------------------------------
/ch01/copyright.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Copyright
6 |
13 |
14 |
15 |
16 | © PHP 8 Solutions
17 |
18 |
--------------------------------------------------------------------------------
/ch02/phpinfo.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Embedded PHP Code
6 |
19 |
20 |
21 |
22 | Embedded Code
23 | PHP code can be embedded directly in the body of a web page.
24 |
35 |
36 |
--------------------------------------------------------------------------------
/ch03/error.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Variable scope
6 |
7 |
8 |
9 | ';
13 | }
14 | $number = 4;
15 | doubleIt($number)
16 | echo '$number is ' . $number;
17 | ?>
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch04/casting.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Automatic data type conversion
6 |
7 |
8 |
9 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/ch04/data_conversion_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Automatic data type conversion
6 |
7 |
8 |
9 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/ch04/data_tests.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Do... while loop
6 |
7 |
8 |
9 | ";
13 | $i++; // increase counter by 1
14 | } while ($i <= 100);
15 | ?>
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch04/foreach_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Foreach loop - shopping list
6 |
7 |
8 |
9 | ';
13 | }
14 | ?>
15 |
16 |
17 |
--------------------------------------------------------------------------------
/ch04/foreach_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Foreach loop - book
6 |
7 |
8 |
9 | 'PHP 8 Solutions: Dynamic Web Design Made Easy',
12 | 'author' => 'David Powers',
13 | 'publisher' => 'Apress'
14 | ];
15 | foreach ($book as $key => $value) {
16 | echo "$key: $value ";
17 | }
18 | ?>
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch04/forloop.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | For loop
6 |
7 |
8 |
9 | ";
12 | }
13 | ?>
14 |
15 |
16 |
--------------------------------------------------------------------------------
/ch04/functions_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Simple function with no arguments
6 |
7 |
8 |
9 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/ch04/functions_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Simple function with argument - no return value
6 |
7 |
8 |
9 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch04/functions_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Simple function with argument - no return value
6 |
7 |
8 |
9 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch04/functions_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Simple function with default - no return value
6 |
7 |
8 |
9 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/ch04/functions_05.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Variable scope
6 |
7 |
8 |
9 | '; // number is doubled
13 | }
14 | $number = 4;
15 | doubleIt($number);
16 | echo 'Outside the function $number is still ' . $number; // not doubled
17 | ?>
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch04/functions_06.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Return value
6 |
7 |
8 |
9 | '; // remains unchanged
16 | echo '$doubled is: ' . $doubled; // original number doubled
17 | ?>
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch04/functions_07.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Passing by reference
6 |
7 |
8 |
9 | ';
15 | doubleIt($num);
16 | echo '$num is now: ' . $num;
17 | ?>
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch04/functions_08.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Arbitrary number of arguments
6 |
7 |
8 |
9 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch04/functions_09.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Type declaration
6 |
7 |
8 |
9 | ';
16 | echo '$doubled is: ' . $doubled;
17 | ?>
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch04/functions_10.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Type declaration
6 |
7 |
8 |
9 | ';
16 | echo '$doubled is: ';
17 | var_dump($doubled);
18 | ?>
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch04/generator.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Generator
6 |
7 |
8 |
9 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ch04/heredoc.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Heredoc syntax
6 |
7 |
8 |
9 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/ch04/inspect_array.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Multidimensional array: print_r()
6 |
7 |
8 |
9 | 'PHP 8 Solutions: Dynamic Web Design Made Easy',
13 | 'author' => 'David Powers'
14 | ],
15 | [
16 | 'title' => 'PHP 8 Revealed',
17 | 'author' => 'Gunnard Engebreth'
18 | ]
19 | ];
20 | print_r($books);
21 | ?>
22 |
23 |
24 |
--------------------------------------------------------------------------------
/ch04/phptest.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/ch04/quotes_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Enclosing Variables in Single Quotes
6 |
7 |
8 |
9 |
14 |
15 |
--------------------------------------------------------------------------------
/ch04/quotes_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Enclosing Variables in Double Quotes
6 |
7 |
8 |
9 |
14 |
15 |
--------------------------------------------------------------------------------
/ch04/quotes_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Enclosing Array Elements in Double Quotes
6 |
7 |
8 |
9 | 'PHP 8 Solutions: Dynamic Web Design Made Easy',
12 | 'author' => 'David Powers',
13 | 'publisher' => 'Apress'
14 | ];
15 | echo "$book['title'] was written by $book['author'].";
16 | ?>
17 |
18 |
--------------------------------------------------------------------------------
/ch04/quotes_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Enclosing Array Elements in Double Quotes
6 |
7 |
8 |
9 | 'PHP 8 Solutions: Dynamic Web Design Made Easy',
12 | 'author' => 'David Powers',
13 | 'publisher' => 'Apress'
14 | ];
15 | echo "{$book['title']} was written by {$book['author']}.";
16 | ?>
17 |
18 |
--------------------------------------------------------------------------------
/ch04/unpack.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Unpacking an array
6 |
7 |
8 |
9 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch04/variable_variables.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Variable variables
6 |
7 |
8 |
9 | ';
13 | ?>
14 |
15 |
--------------------------------------------------------------------------------
/ch04/while.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | While loop
6 |
7 |
8 |
9 | ";
13 | $i++; // increase counter by 1
14 | }
15 | ?>
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch05/contact.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Japan Journey
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 | Contact Us
17 | Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.
18 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/ch05/document_root.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Document root test
6 |
7 |
8 |
9 |
10 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch05/error.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Japan Journey
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
21 |
22 |
23 | Oops! There’s a Problem
24 | Sorry, the page you were looking for cannot be displayed.
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/ch05/footer_01.php:
--------------------------------------------------------------------------------
1 |
2 | © 2006–2021 David Powers
3 |
--------------------------------------------------------------------------------
/ch05/footer_02.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/ch05/gallery.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Japan Journey
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 | Images of Japan
16 | Displaying 1 to 6 of 8
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | Maiko—trainee geishas in Kyoto
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/ch05/gallery_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Japan Journey = $title ?>
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 | Images of Japan
17 | Displaying 1 to 6 of 8
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | Maiko—trainee geishas in Kyoto
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ch05/gallery_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Japan Journey = $title ?? '' ?>
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 | Images of Japan
17 | Displaying 1 to 6 of 8
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | Maiko—trainee geishas in Kyoto
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ch05/get_filename.php:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/ch05/menu_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch05/random_image_01.php:
--------------------------------------------------------------------------------
1 | 'kinkakuji',
4 | 'caption' => 'The Golden Pavilion in Kyoto'],
5 | ['file' => 'maiko',
6 | 'caption' => 'Maiko—trainee geishas in Kyoto'],
7 | ['file' => 'maiko_phone',
8 | 'caption' => 'Every maiko should have one—a mobile, of course'],
9 | ['file' => 'monk',
10 | 'caption' => 'Monk begging for alms in Kyoto'],
11 | ['file' => 'fountains',
12 | 'caption' => 'Fountains in central Tokyo'],
13 | ['file' => 'ryoanji',
14 | 'caption' => 'Autumn leaves at Ryoanji temple, Kyoto'],
15 | ['file' => 'menu',
16 | 'caption' => 'Menu outside restaurant in Pontocho, Kyoto'],
17 | ['file' => 'basin',
18 | 'caption' => 'Water basin at Ryoanji temple, Kyoto']
19 | ];
20 | $i = random_int(0, count($images)-1);
21 | $selectedImage = "images/{$images[$i]['file']}.jpg";
22 | $caption = $images[$i]['caption'];
23 | if (file_exists($selectedImage) && is_readable($selectedImage)) {
24 | $imageSize = getimagesize($selectedImage);
25 | }
26 |
--------------------------------------------------------------------------------
/ch05/title.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | XSS Link
6 |
7 |
8 |
9 | Go
10 | on, trust me
11 |
12 |
--------------------------------------------------------------------------------
/ch06/bad_link_end.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | XSS Link
6 |
7 |
8 |
9 | Go
11 | on, trust me
12 |
13 |
--------------------------------------------------------------------------------
/ch06/error.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Japan Journey
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
21 |
22 |
23 | Oops! There’s a Problem
24 | Sorry, the page you were looking for cannot be displayed.
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/ch06/form.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | PHP_SELF XSS
6 |
7 |
8 |
9 | Prevent XSS with Form
10 | Hi there, ' . htmlentities($_POST['name']) . '!';
13 | }
14 | ?>
15 |
24 |
25 |
--------------------------------------------------------------------------------
/ch06/form_end.php:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 |
11 |
12 | PHP_SELF XSS
13 |
14 |
15 |
16 | Prevent XSS with Form
17 | Hi there, ' . htmlentities($_POST['name']) . '!';
20 | }
21 | ?>
22 |
31 |
32 |
--------------------------------------------------------------------------------
/ch06/gallery.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Japan Journey = $title ?? '' ?>
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 | Images of Japan
17 | Displaying 1 to 6 of 8
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | Maiko—trainee geishas in Kyoto
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ch06/includes/footer.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/ch06/includes/menu.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch06/includes/processmail_01.php:
--------------------------------------------------------------------------------
1 | $value) {
3 | // strip whitespace from $value if not an array
4 | if (!is_array($value)) {
5 | $value = trim($value);
6 | }
7 | if (!in_array($key, $expected)) {
8 | // ignore the value, it's not in $expected
9 | continue;
10 | }
11 | if (in_array($key, $required) && empty($value)) {
12 | // required value is missing
13 | $missing[] = $key;
14 | $$key = "";
15 | continue;
16 | }
17 | $$key = $value;
18 | }
19 |
--------------------------------------------------------------------------------
/ch06/includes/processmail_02.php:
--------------------------------------------------------------------------------
1 | $value) {
8 | // strip whitespace from $value if not an array
9 | if (!is_array($value)) {
10 | $value = trim($value);
11 | }
12 | if (!in_array($key, $expected)) {
13 | // ignore the value, it's not in $expected
14 | continue;
15 | }
16 | if (in_array($key, $required) && empty($value)) {
17 | // required value is missing
18 | $missing[] = $key;
19 | $$key = "";
20 | continue;
21 | }
22 | $$key = $value;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/ch06/includes/processmail_03.php:
--------------------------------------------------------------------------------
1 | $value) {
8 | // strip whitespace from $value if not an array
9 | if (!is_array($value)) {
10 | $value = trim($value);
11 | }
12 | if (!in_array($key, $expected)) {
13 | // ignore the value, it's not in $expected
14 | continue;
15 | }
16 | if (in_array($key, $required) && empty($value)) {
17 | // required value is missing
18 | $missing[] = $key;
19 | $$key = "";
20 | continue;
21 | }
22 | $$key = $value;
23 | }
24 | }
25 | // validate the user's email
26 | if (!$suspect && !empty($email)) {
27 | $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
28 | if ($validemail) {
29 | $headers['Reply-To'] = $validemail;
30 | } else {
31 | $errors['email'] = true;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/ch06/includes/random_image.php:
--------------------------------------------------------------------------------
1 | 'kinkakuji',
4 | 'caption' => 'The Golden Pavilion in Kyoto'],
5 | ['file' => 'maiko',
6 | 'caption' => 'Maiko—trainee geishas in Kyoto'],
7 | ['file' => 'maiko_phone',
8 | 'caption' => 'Every maiko should have one—a mobile, of course'],
9 | ['file' => 'monk',
10 | 'caption' => 'Monk begging for alms in Kyoto'],
11 | ['file' => 'fountains',
12 | 'caption' => 'Fountains in central Tokyo'],
13 | ['file' => 'ryoanji',
14 | 'caption' => 'Autumn leaves at Ryoanji temple, Kyoto'],
15 | ['file' => 'menu',
16 | 'caption' => 'Menu outside restaurant in Pontocho, Kyoto'],
17 | ['file' => 'basin',
18 | 'caption' => 'Water basin at Ryoanji temple, Kyoto']
19 | ];
20 | $i = random_int(0, count($images)-1);
21 | $selectedImage = "images/{$images[$i]['file']}.jpg";
22 | $caption = $images[$i]['caption'];
23 | if (file_exists($selectedImage) && is_readable($selectedImage)) {
24 | $imageSize = getimagesize($selectedImage);
25 | }
26 |
--------------------------------------------------------------------------------
/ch06/includes/title.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Missing Page
6 |
7 |
8 |
9 | Oops!
10 | The page you're looking for appears to be missing.
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ch06/thank_you.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Japan Journey= $title ?? '' ?>
6 |
7 |
8 |
9 |
10 |
11 | Japan Journey
12 |
13 |
14 |
15 |
16 | Thank You
17 | Your message has been sent. We appreciate your feedback, and will be in touch, if necessary.
18 | We hope you'll continue to enjoy browsing the Japan Journey website.
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/ch07/buildlist.php:
--------------------------------------------------------------------------------
1 | getFilename();
15 | }
16 | natcasesort($filenames);
17 | return $filenames;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/ch07/download.php:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
15 |
16 | Append Mode
17 |
29 |
30 |
31 |
32 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch07/fopen_read.php:
--------------------------------------------------------------------------------
1 | tags
11 | echo nl2br($contents);
--------------------------------------------------------------------------------
/ch07/fopen_readloop.php:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
15 |
16 | Write Only
17 |
29 |
30 |
31 |
32 |
33 |
34 | Write this to file:
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch07/get_contents_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Read File
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ch07/get_contents_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Read File with nl2br()
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ch07/get_contents_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Get File Contents
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ch07/get_contents_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Processing File Content
6 |
7 |
8 |
19 |
20 |
--------------------------------------------------------------------------------
/ch07/get_contents_05.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Load File as an Array
6 |
7 |
8 |
12 |
13 |
--------------------------------------------------------------------------------
/ch07/get_contents_06.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Array Dereferencing
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ch07/getcsv.php:
--------------------------------------------------------------------------------
1 | ';
14 | print_r($cities);
15 | echo '';
--------------------------------------------------------------------------------
/ch07/getdownloads.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Download Images
6 |
7 |
8 | Download fountains image
9 | Download monk image
10 |
11 |
12 |
--------------------------------------------------------------------------------
/ch07/imagelist_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Dynamically Generated Image Menu
6 |
7 |
8 |
9 |
10 |
11 | Select an image
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch07/imagelist_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Dynamically Generated Image Menu
6 |
7 |
8 |
9 |
10 |
11 | Select an image
12 | getFilename();
17 | ?>
18 | = $filename ?>
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/ch07/iterator_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Using FilesystemIterator
6 |
7 |
8 |
9 |
10 | ';
14 | }
15 | ?>
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch07/iterator_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Using FilesystemIterator
6 |
7 |
8 |
9 |
10 | ';
14 | }
15 | ?>
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch07/iterator_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Using FilesystemIterator
6 |
7 |
8 |
9 |
10 | setFlags(FilesystemIterator::UNIX_PATHS);
13 | foreach ($files as $file) {
14 | echo $file . ' ';
15 | }
16 | ?>
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch07/iterator_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | RecursiveDirectoryIterator
6 |
7 |
8 |
9 |
10 | setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
13 | $files = new RecursiveIteratorIterator($files);
14 | foreach ($files as $file) {
15 | echo $file->getRealPath() . ' ';
16 | }
17 | ?>
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch07/iterator_05.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Filtering with the RegexIterator
6 |
7 |
8 |
9 |
10 | getFilename() . ' ';
15 | }
16 | ?>
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch07/newsfeed.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Displaying an RSS Feed
6 |
7 |
8 |
9 |
10 | The Latest from BBC News
11 | channel->item, 0, 4);
15 | foreach ($filtered as $item) { ?>
16 |
18 | pubDate);
19 | $date->setTimezone(new DateTimeZone('America/New_York'));
20 | $offset = $date->getOffset();
21 | $timezone = ($offset == -14400) ? ' EDT' : ' EST';
22 | echo $date->format('M j, Y, g:ia') . $timezone; ?>
23 | = htmlentities($item->description) ?>
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch07/scandir.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Using scandir()
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/ch07/sonnet.txt:
--------------------------------------------------------------------------------
1 | Let me not to the marriage of true minds
2 | Admit impediments; love is not love
3 | Which alters when it alteration finds,
4 | Or bends with the remover to remove.
5 | Oh, no, it is an ever fixed mark
6 | That looks on tempests and is never shaken;
7 | It is the star to every wand'ring bark,
8 | Whose worth's unknown, although his height be taken.
9 | Love's not Time's fool, though rosy lips and cheeks
10 | Within his bending sickle's compass come;
11 | Love alters not with his brief hours and weeks,
12 | But bears it out even to the edge of Doom.
13 | If this be error and upon me proved,
14 | I never writ, nor no man ever loved.
--------------------------------------------------------------------------------
/ch07/weather.csv:
--------------------------------------------------------------------------------
1 | city,temp
2 | London,11
3 | Paris,10
4 | Rome,12
5 | Berlin,8
6 | Athens,19
--------------------------------------------------------------------------------
/ch08/array_change_key_case_01.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'Title' => 'PHP 8 Solutions'
5 | ];
6 | $book = array_change_key_case($book);
7 | echo '';
8 | print_r($book);
9 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_change_key_case_02.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'Title' => 'PHP 8 Solutions'
5 | ];
6 | $book = array_change_key_case($book, CASE_UPPER);
7 | echo '';
8 | print_r($book);
9 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_combine.php:
--------------------------------------------------------------------------------
1 | 'stop', 'amber' => 'caution', 'green' => 'go]
6 | echo '';
7 | print_r($signals);
8 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_diff.php:
--------------------------------------------------------------------------------
1 | ';
12 | print_r($diff);
13 | echo '';
--------------------------------------------------------------------------------
/ch08/array_diff_assoc.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf',
4 | 'JavaScript' => 'Brendan Eich',
5 | 'R' => 'Robert Gentleman'];
6 | $second = [
7 | 'Java' => 'James Gosling',
8 | 'R' => 'Ross Ihaka',
9 | 'Python' => 'Guido van Rossum'];
10 | /*
11 | * array_diff_assoc() checks both keys and values,
12 | * returning an array of elements that exist in the
13 | * first array, but not in others. In this example,
14 | * all three elements in the first array are returned,
15 | * even though both arrays contain R as a key. This is
16 | * because the values assigned to R are different.
17 | */
18 | $diff = array_diff_assoc($first, $second);
19 | echo '';
20 | print_r($diff);
21 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_diff_key.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf',
4 | 'JavaScript' => 'Brendan Eich',
5 | 'R' => 'Robert Gentleman'];
6 | $second = [
7 | 'Java' => 'James Gosling',
8 | 'R' => 'Ross Ihaka',
9 | 'Python' => 'Guido van Rossum'];
10 | /*
11 | * array_diff_key() checks only the keys, ignoring the values.
12 | * As a result, this returns the first two elements of the first
13 | * array, but not the third because R exists as a key in the
14 | * second array. The fact that the values assigned to R are
15 | * different is irrelevant.
16 | */
17 | $diff = array_diff_key($first, $second);
18 | echo '';
19 | print_r($diff);
20 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_diff_uassoc.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf',
4 | 'JavaScript' => 'Brendan Eich',
5 | 'R' => 'Robert Gentleman'];
6 | $second = [
7 | 'java' => 'James Gosling',
8 | 'r' => 'Ross Ihaka',
9 | 'php' => 'Rasmus Lerdorf'];
10 | /*
11 | * array_diff_uassoc() checks both keys and values,
12 | * using a callback function to compare the keys.
13 | * The callback supplied here is strcasecmp() which
14 | * makes a case-insensitive comparison, returning 0
15 | * if both strings are equal. This returns the last
16 | * two elements in the first array. Although R exists
17 | * as a key (both uppercase and lowercase) in both
18 | * arrays, the values are different, so it's
19 | * included in the result.
20 | */
21 | $diff = array_diff_uassoc($first, $second, 'strcasecmp');
22 | echo '';
23 | print_r($diff);
24 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_diff_ukey.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf',
4 | 'JavaScript' => 'Brendan Eich',
5 | 'R' => 'Robert Gentleman'];
6 | $second = [
7 | 'java' => 'James Gosling',
8 | 'r' => 'Ross Ihaka',
9 | 'python' => 'Guido van Rossum'];
10 | /*
11 | * array_diff_ukey() checks only the keys, ignoring the values;
12 | * and it uses a callback function to compare the keys. The callback
13 | * supplied here is strcasecmp() which makes a case-insensitive
14 | * comparison, returning 0 if both strings are equal. As a result,
15 | * this returns the first two elements of the first array, but not
16 | * the third because lowercase R exists as a key in the second array.
17 | * The fact that the values assigned to R are different is irrelevant.
18 | */
19 | $diff = array_diff_ukey($first, $second, 'strcasecmp');
20 | echo '';
21 | print_r($diff);
22 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_intersect.php:
--------------------------------------------------------------------------------
1 | ';
13 | print_r($common);
14 | echo '';
--------------------------------------------------------------------------------
/ch08/array_intersect_assoc.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich', 'R' => 'Robert Gentleman'];
3 | $second = ['Java' => 'James Gosling', 'Python' => 'Guido van Rossum', 'PHP' => 'Rasmus Lerdorf'];
4 | /*
5 | * array_intersect_assoc() checks both key and value.
6 | * Although Rasmus Lerdorf appears in both arrays,
7 | * this returns an empty array because the keys don't
8 | * match (the first is lowercase, whereas the second is
9 | * uppercase).
10 | */
11 | $common = array_intersect_assoc($first, $second);
12 | echo '';
13 | print_r($common);
14 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_intersect_key.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich', 'R' => 'Robert Gentleman'];
3 | $second = ['Java' => 'James Gosling', 'R' => 'Ross Ihaka', 'Python' => 'Guido van Rossum'];
4 | /*
5 | * array_intersect_key() checks only for matching keys.
6 | * As a result, Robert Gentleman is returned from the
7 | * first array, even though the value of R is different
8 | * in the second array.
9 | */
10 | $common = array_intersect_key($first, $second);
11 | echo '';
12 | print_r($common);
13 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_intersect_uassoc.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf',
4 | 'JavaScript' => 'Brendan Eich',
5 | 'R' => 'Robert Gentleman'];
6 | $second = [
7 | 'Java' => 'James Gosling',
8 | 'Python' => 'Guido van Rossum',
9 | 'PHP' => 'Rasmus Lerdorf'];
10 | /*
11 | * array_intersect_uassoc() checks both keys and values, and
12 | * uses a callback function to compare the keys. The callback
13 | * supplied here is strcasecmp() which makes a case-insensitive
14 | * comparison, returning 0 if both strings are equal. As a
15 | * result, this example returns Rasmus Lerdorf from the first
16 | * array, even though the keys don't use matching cases.
17 | */
18 | $common = array_intersect_uassoc($first, $second, 'strcasecmp');
19 | echo '';
20 | print_r($common);
21 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_intersect_ukey.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf',
4 | 'JavaScript' => 'Brendan Eich',
5 | 'R' => 'Robert Gentleman'];
6 | $second = [
7 | 'java' => 'James Gosling',
8 | 'r' => 'Ross Ihaka',
9 | 'python' => 'Guido van Rossum'];
10 | /*
11 | * array_intersect_ukey() checks only for matching keys,
12 | * using a callback function to compare them. The callback
13 | * supplied here is strcasecmp() which makes a case-insensitive
14 | * comparison, returning 0 if both strings are equal. As a
15 | * result, this example returns Robert Gentleman from the first
16 | * array, even though R is uppercase in the first array and
17 | * lowercase in the second.
18 | */
19 | $common = array_intersect_ukey($first, $second, 'strcasecmp');
20 | echo '';
21 | print_r($common);
22 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_map_01.php:
--------------------------------------------------------------------------------
1 | $num * 2, $numbers);
4 | echo '';
5 | print_r($numbers);
6 | print_r($doubled);
7 | echo ' ';
8 |
--------------------------------------------------------------------------------
/ch08/array_map_02.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | $modified = array_map('modify', $book);
7 | function modify($val) {
8 | return strtoupper($val);
9 | }
10 | echo '';
11 | print_r($book);
12 | print_r($modified);
13 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_map_03.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | $descriptions = ['British', 'the fifth edition'];
7 | $modified = array_map('modify', $book, $descriptions);
8 | function modify($val, $description) {
9 | return "$val is $description.";
10 | }
11 | echo '';
12 | print_r($book);
13 | print_r($modified);
14 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_map_04.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | $descriptions = ['British', 'the fifth edition'];
7 | $label = ['Description'];
8 | $modified = array_map('modify', $book, $descriptions, $label);
9 | function modify($val, $description, $label) {
10 | return "$label: $val is $description.";
11 | }
12 | echo '';
13 | print_r($book);
14 | print_r($modified);
15 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_walk_01.php:
--------------------------------------------------------------------------------
1 | $val *= 2);
4 | echo '';
5 | print_r($numbers);
6 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_walk_02.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | array_walk($book, fn (&$val) => $val = strtoupper($val));
7 | echo '';
8 | print_r($book);
9 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_walk_03.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | array_walk($book, 'output');
7 | function output (&$val) {
8 | return $val = strtoupper($val);
9 | }
10 | echo '';
11 | print_r($book);
12 | echo ' ';
--------------------------------------------------------------------------------
/ch08/array_walk_04.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | array_walk($book, 'output', 'is');
7 | function output (&$val, $key, $verb) {
8 | return $val = "The $key of this book $verb $val.";
9 | }
10 | echo '';
11 | print_r($book);
12 | echo ' ';
--------------------------------------------------------------------------------
/ch08/commas_01.php:
--------------------------------------------------------------------------------
1 | '',
13 | $length === 1 => array_pop($array),
14 | $length === 2 => implode(' and ', $array),
15 | $length === $max + 1 => implode(', ', array_slice($array, 0, $max)) . ' and one other',
16 | $length > $max + 1 => implode(', ', array_slice($array, 0, $max)) . ' and others',
17 | default => implode(', ', array_slice($array, 0, $length -1)) . ' and ' . array_pop($array)
18 | };
19 | return $result;
20 | }
21 | echo with_commas($too_many, 3);
--------------------------------------------------------------------------------
/ch08/csv_list_01.php:
--------------------------------------------------------------------------------
1 | ";
8 | }
--------------------------------------------------------------------------------
/ch08/csv_list_02.php:
--------------------------------------------------------------------------------
1 | next();
6 | while ($scores->valid()) {
7 | [$home, $hscore, $away, $ascore] = $scores->current();
8 | echo "$home $hscore:$ascore $away ";
9 | $scores->next();
10 | }
--------------------------------------------------------------------------------
/ch08/csv_processor.php:
--------------------------------------------------------------------------------
1 | next();
11 | while ($cities->valid()) {
12 | echo display_temp(...$cities->current()) . ' ';
13 | $cities->next();
14 | }
--------------------------------------------------------------------------------
/ch08/csv_splat_02.php:
--------------------------------------------------------------------------------
1 | next();
11 | while ($cities->valid()) {
12 | [$city, $temp] = $cities->current();
13 | echo display_temp($city, $temp) . ' ';
14 | $cities->next();
15 | }
--------------------------------------------------------------------------------
/ch08/data/scores.csv:
--------------------------------------------------------------------------------
1 | Home team,Home score,Away team,Away score
2 | Arsenal,2,Newcastle United,0
3 | Tottenham Hotspur,2,Crystal Palace,0
4 | Watford,4,Fulham,1
5 | Manchester City,2,Cardiff City,0
6 | Southampton,1,Liverpool,3
7 | Wolverhampton Wanderers,2,Manchester United,1
8 |
--------------------------------------------------------------------------------
/ch08/data/weather.csv:
--------------------------------------------------------------------------------
1 | city,temp
2 | London,11
3 | Paris,10
4 | Rome,12
5 | Berlin,8
6 | Athens,19
--------------------------------------------------------------------------------
/ch08/json_01.php:
--------------------------------------------------------------------------------
1 | ';
7 | print_r($data);
8 | echo '';
9 |
--------------------------------------------------------------------------------
/ch08/json_02.php:
--------------------------------------------------------------------------------
1 | str_contains($location['Locations'], $search);
13 | $filtered = array_filter($locations, $getLocation);
14 | echo '';
15 | foreach ($filtered as $item) {
16 | echo "{$item['Title']} ({$item['Release Year']}) filmed at {$item['Locations']} ";
17 | }
18 | echo ' ';
19 |
--------------------------------------------------------------------------------
/ch08/json_04.php:
--------------------------------------------------------------------------------
1 | str_contains($location['Locations'], $search);
13 | $filtered = array_filter($locations, $getLocation);
14 | echo '';
15 | $duplicates = [];
16 | foreach ($filtered as $item) {
17 | if (in_array($item['Title'], $duplicates)) continue;
18 | echo "{$item['Title']} ({$item['Release Year']}) filmed at {$item['Locations']} ";
19 | $duplicates[] = $item['Title'];
20 | }
21 | echo ' ';
22 |
--------------------------------------------------------------------------------
/ch08/list_01.php:
--------------------------------------------------------------------------------
1 | 'David',
4 | 'last name' => 'Powers',
5 | 'city' => 'London',
6 | 'country' => 'the UK'];
7 | list('country' => $country,
8 | 'last name' => $surname,
9 | 'first name' => $name) = $person;
10 | // Displays "David Powers lives in the UK."
11 | echo "$name $surname lives in $country.";
12 |
--------------------------------------------------------------------------------
/ch08/list_03.php:
--------------------------------------------------------------------------------
1 | 'David',
4 | 'last name' => 'Powers',
5 | 'city' => 'London',
6 | 'country' => 'the UK'];
7 | ['country' => $country, 'last name' => $surname, 'first name' => $name] = $person;
8 | // Displays "David Powers lives in the UK."
9 | echo "$name $surname lives in $country.";
10 |
--------------------------------------------------------------------------------
/ch08/merge_01.php:
--------------------------------------------------------------------------------
1 | ';
6 | print_r($languages);
7 | echo '';
--------------------------------------------------------------------------------
/ch08/merge_02.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich'];
3 | $second = ['Java' => 'James Gosling', 'R' => 'Ross Ihaka', 'Python' => 'Guido van Rossum'];
4 | $lead_developers = $first + $second;
5 | echo '';
6 | print_r($lead_developers);
7 | echo ' ';
--------------------------------------------------------------------------------
/ch08/merge_03.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich', 'R' => 'Robert Gentleman'];
3 | $second = ['Java' => 'James Gosling', 'R' => 'Ross Ihaka', 'Python' => 'Guido van Rossum'];
4 | $lead_developers = $first + $second;
5 | echo '';
6 | print_r($lead_developers);
7 | echo ' ';
--------------------------------------------------------------------------------
/ch08/merge_04.php:
--------------------------------------------------------------------------------
1 | ';
6 | print_r($languages);
7 | echo '';
--------------------------------------------------------------------------------
/ch08/merge_05.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich'];
3 | $second = ['Java' => 'James Gosling', 'R' => 'Ross Ihaka', 'Python' => 'Guido van Rossum'];
4 | $lead_developers = array_merge($first, $second);
5 | echo '';
6 | print_r($lead_developers);
7 | echo ' ';
--------------------------------------------------------------------------------
/ch08/merge_06.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich', 'R' => 'Robert Gentleman'];
3 | $second = ['Java' => 'James Gosling', 'R' => 'Ross Ihaka', 'Python' => 'Guido van Rossum'];
4 | $lead_developers = array_merge($first, $second);
5 | echo '';
6 | print_r($lead_developers);
7 | echo ' ';
--------------------------------------------------------------------------------
/ch08/merge_07.php:
--------------------------------------------------------------------------------
1 | 'Rasmus Lerdorf', 'JavaScript' => 'Brendan Eich', 'R' => 'Robert Gentleman'];
3 | $second = ['Java' => 'James Gosling', 'R' => 'Ross Ihaka', 'Python' => 'Guido van Rossum'];
4 | $lead_developers = array_merge_recursive($first, $second);
5 | echo '';
6 | print_r($lead_developers);
7 | echo ' ';
--------------------------------------------------------------------------------
/ch08/modify_01.php:
--------------------------------------------------------------------------------
1 | ';
7 | print_r($numbers);
8 | echo '';
9 |
--------------------------------------------------------------------------------
/ch08/modify_02.php:
--------------------------------------------------------------------------------
1 | ';
8 | print_r($numbers);
9 | echo '';
--------------------------------------------------------------------------------
/ch08/modify_03.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | foreach ($book as $key => &$value) {
7 | $book[$key] = strtoupper($value);
8 | }
9 | unset($value);
10 | echo '';
11 | print_r($book);
12 | echo ' ';
--------------------------------------------------------------------------------
/ch08/modify_04.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | foreach ($book as $key => $value) {
7 | $book[ucfirst($key)] = $value;
8 | }
9 | unset($value);
10 | echo '';
11 | print_r($book);
12 | echo ' ';
--------------------------------------------------------------------------------
/ch08/modify_05.php:
--------------------------------------------------------------------------------
1 | 'David Powers',
4 | 'title' => 'PHP 8 Solutions'
5 | ];
6 | foreach ($book as $key => $value) {
7 | $book[ucfirst($key)] = $value;
8 | unset($book[$key]);
9 | }
10 | unset($value);
11 | echo '';
12 | print_r($book);
13 | echo ' ';
--------------------------------------------------------------------------------
/ch08/multidimensional_01.php:
--------------------------------------------------------------------------------
1 | [
4 | 'California' => [
5 | 'Chardonnay', 'Sauvignon blanc'
6 | ],
7 | 'Burgundy' => [
8 | 'Chardonnay', 'Pinot blanc'
9 | ]
10 | ],
11 | 'Red' => [
12 | 'California' => [
13 | 'Merlot', 'Zinfandel'
14 | ],
15 | 'Burgundy' => [
16 | 'Pinot noir', 'Gamay'
17 | ]
18 | ]
19 | ];
--------------------------------------------------------------------------------
/ch08/multidimensional_02.php:
--------------------------------------------------------------------------------
1 | [
4 | 'California' => [
5 | 'Chardonnay', 'Sauvignon blanc'
6 | ],
7 | 'Burgundy' => [
8 | 'Chardonnay', 'Pinot blanc'
9 | ]
10 | ],
11 | 'Red' => [
12 | 'California' => [
13 | 'Merlot', 'Zinfandel'
14 | ],
15 | 'Burgundy' => [
16 | 'Pinot noir', 'Gamay'
17 | ]
18 | ]
19 | ];
20 |
21 | /*
22 | * Rename ListBuilder_end.php if you haven't created
23 | * your own definition of the ListBuilder class.
24 | */
25 | require './ListBuilder_end.php';
26 | echo new ListBuilder($wines);
27 |
--------------------------------------------------------------------------------
/ch08/multisort_01.php:
--------------------------------------------------------------------------------
1 | ';
5 | for ($i = 0, $len = count($states); $i < $len; $i++) {
6 | echo "$states[$i]: $population[$i] ";
7 | }
8 | echo '';
--------------------------------------------------------------------------------
/ch08/multisort_02.php:
--------------------------------------------------------------------------------
1 | ';
6 | for ($i = 0, $len = count($states); $i < $len; $i++) {
7 | echo "$states[$i]: $population[$i] ";
8 | }
9 | echo '';
--------------------------------------------------------------------------------
/ch08/multisort_03.php:
--------------------------------------------------------------------------------
1 | 'Jethro Tull', 'track' => 'Locomotive Breath', 'rating' => 8],
4 | ['artist' => 'Dire Straits', 'track' => 'Telegraph Road', 'rating' => 7],
5 | ['artist' => 'Mumford and Sons', 'track' => 'Broad-Shouldered Beasts', 'rating' => 9],
6 | ['artist' => 'Ed Sheeran', 'track' => 'Nancy Mulligan', 'rating' => 10],
7 | ['artist' => 'Dire Straits', 'track' => 'Sultans of Swing', 'rating' => 9],
8 | ['artist' => 'Jethro Tull', 'track' => 'Aqualung', 'rating' => 10],
9 | ['artist' => 'Mumford and Sons', 'track' => 'Thistles and Weeds', 'rating' => 6],
10 | ['artist' => 'Ed Sheeran', 'track' => 'Eraser', 'rating' => 8]
11 | ];
--------------------------------------------------------------------------------
/ch08/multisort_04.php:
--------------------------------------------------------------------------------
1 | 'Jethro Tull', 'track' => 'Locomotive Breath', 'rating' => 8],
4 | ['artist' => 'Dire Straits', 'track' => 'Telegraph Road', 'rating' => 7],
5 | ['artist' => 'Mumford and Sons', 'track' => 'Broad-Shouldered Beasts', 'rating' => 9],
6 | ['artist' => 'Ed Sheeran', 'track' => 'Nancy Mulligan', 'rating' => 10],
7 | ['artist' => 'Dire Straits', 'track' => 'Sultans of Swing', 'rating' => 9],
8 | ['artist' => 'Jethro Tull', 'track' => 'Aqualung', 'rating' => 10],
9 | ['artist' => 'Mumford and Sons', 'track' => 'Thistles and Weeds', 'rating' => 6],
10 | ['artist' => 'Ed Sheeran', 'track' => 'Eraser', 'rating' => 8]
11 | ];
12 | $tracks = array_column($playlist, 'track');
13 | $ratings = array_column($playlist, 'rating');
14 | print_r($tracks);
15 | print_r($ratings);
--------------------------------------------------------------------------------
/ch08/multisort_05.php:
--------------------------------------------------------------------------------
1 | 'Jethro Tull', 'track' => 'Locomotive Breath', 'rating' => 8],
4 | ['artist' => 'Dire Straits', 'track' => 'Telegraph Road', 'rating' => 7],
5 | ['artist' => 'Mumford and Sons', 'track' => 'Broad-Shouldered Beasts', 'rating' => 9],
6 | ['artist' => 'Ed Sheeran', 'track' => 'Nancy Mulligan', 'rating' => 10],
7 | ['artist' => 'Dire Straits', 'track' => 'Sultans of Swing', 'rating' => 9],
8 | ['artist' => 'Jethro Tull', 'track' => 'Aqualung', 'rating' => 10],
9 | ['artist' => 'Mumford and Sons', 'track' => 'Thistles and Weeds', 'rating' => 6],
10 | ['artist' => 'Ed Sheeran', 'track' => 'Eraser', 'rating' => 8]
11 | ];
12 | $tracks = array_column($playlist, 'track');
13 | $ratings = array_column($playlist, 'rating');
14 | array_multisort($ratings, SORT_DESC, $tracks, SORT_ASC, $playlist);
15 | echo '';
16 | foreach ($playlist as $item) {
17 | echo "{$item['rating']} {$item['track']} by {$item['artist']} ";
18 | }
19 | echo ' ';
20 |
--------------------------------------------------------------------------------
/ch08/natsort.php:
--------------------------------------------------------------------------------
1 | ';
5 | echo 'sort(): ';
6 | print_r($images);
7 | natsort($images);
8 | echo 'natsort(): ';
9 | print_r($images);
10 | echo '';
--------------------------------------------------------------------------------
/ch08/permutations.php:
--------------------------------------------------------------------------------
1 | ';
22 | }
--------------------------------------------------------------------------------
/ch08/spaceship_01.php:
--------------------------------------------------------------------------------
1 | 'Jethro Tull', 'track' => 'Locomotive Breath'],
4 | ['artist' => 'Dire Straits', 'track' => 'Telegraph Road'],
5 | ['artist' => 'Mumford and Sons', 'track' => 'Broad-Shouldered Beasts'],
6 | ['artist' => 'Ed Sheeran', 'track' => 'Nancy Mulligan'],
7 | ['artist' => 'Dire Straits', 'track' => 'Sultans of Swing'],
8 | ['artist' => 'Jethro Tull', 'track' => 'Aqualung'],
9 | ['artist' => 'Mumford and Sons', 'track' => 'Thistles and Weeds'],
10 | ['artist' => 'Ed Sheeran', 'track' => 'Eraser']
11 | ];
12 | echo '';
13 | foreach ($playlist as $item) {
14 | echo "{$item['track']}: {$item['artist']} ";
15 | }
16 | echo ' ';
--------------------------------------------------------------------------------
/ch08/spaceship_02.php:
--------------------------------------------------------------------------------
1 | 'Jethro Tull', 'track' => 'Locomotive Breath'],
4 | ['artist' => 'Dire Straits', 'track' => 'Telegraph Road'],
5 | ['artist' => 'Mumford and Sons', 'track' => 'Broad-Shouldered Beasts'],
6 | ['artist' => 'Ed Sheeran', 'track' => 'Nancy Mulligan'],
7 | ['artist' => 'Dire Straits', 'track' => 'Sultans of Swing'],
8 | ['artist' => 'Jethro Tull', 'track' => 'Aqualung'],
9 | ['artist' => 'Mumford and Sons', 'track' => 'Thistles and Weeds'],
10 | ['artist' => 'Ed Sheeran', 'track' => 'Eraser']
11 | ];
12 | echo '';
13 | usort($playlist, fn ($a, $b) => $b['track'] <=> $a['track']);
14 | foreach ($playlist as $item) {
15 | echo "{$item['track']}: {$item['artist']} ";
16 | }
17 | echo ' ';
--------------------------------------------------------------------------------
/ch08/unique_01.php:
--------------------------------------------------------------------------------
1 | 'With a Little Help from my Friends',
4 | 'Joe Cocker' => 'With A Little Help From My Friends',
5 | 'Wet Wet Wet' => 'With a Little Help from my Friends',
6 | 'Paul McCartney' => 'Yesterday'
7 | ];
8 | $unique = array_unique($tracks);
9 | echo '';
10 | print_r($unique);
11 | echo ' ';
--------------------------------------------------------------------------------
/ch09/file_upload_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Upload File
6 |
7 |
8 |
9 |
10 |
11 | Upload image:
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch09/file_upload_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Upload File
6 |
7 |
8 |
9 |
10 |
11 | Upload image:
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ch09/file_upload_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Upload File
6 |
7 |
8 |
9 |
10 |
11 | Upload image:
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch09/file_upload_04.php:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
15 |
16 | Upload File
17 |
18 |
19 |
20 |
21 |
22 | Upload image:
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/ch09/file_upload_05.php:
--------------------------------------------------------------------------------
1 | getMessages();
12 | } catch (Throwable $t) {
13 | echo $t->getMessage();
14 | }
15 | }
16 | ?>
17 |
18 |
19 |
20 |
21 | Upload File
22 |
23 |
24 |
25 | ';
28 | foreach ($result as $message) {
29 | echo "$message ";
30 | }
31 | echo '';
32 | }
33 | ?>
34 |
35 |
36 | Upload image:
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch09/file_upload_06.php:
--------------------------------------------------------------------------------
1 | getMessages();
12 | } catch (Throwable $t) {
13 | echo $t->getMessage();
14 | }
15 | }
16 | ?>
17 |
18 |
19 |
20 |
21 | Upload File
22 |
23 |
24 |
25 | ';
28 | foreach ($result as $message) {
29 | echo "$message ";
30 | }
31 | echo '';
32 | }
33 | ?>
34 |
35 |
36 | Upload image:
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch09/multi_upload.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Multiple Upload
6 |
7 |
8 |
9 |
10 |
11 | Upload images (multiple selections permitted):
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ch10/create_thumb_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Create Thumbnail
6 |
7 |
8 |
9 |
10 |
11 |
12 | Select an image
13 |
17 |
18 | = $image->getFilename() ?>
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch10/create_thumb_02.php:
--------------------------------------------------------------------------------
1 | test();
9 | } catch (Throwable $t) {
10 | echo $t->getMessage();
11 | }
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | Create Thumbnail
19 |
20 |
21 |
22 |
23 |
24 |
25 | Select an image
26 |
30 |
31 | = $image->getFilename() ?>
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/ch10/create_thumb_upload_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Thumbnail Upload
6 |
7 |
8 |
9 | ';
12 | foreach ($messages as $message) {
13 | echo "$message ";
14 | }
15 | echo '';
16 | }
17 | ?>
18 |
19 |
20 | Upload images to create thumbnails:
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ch10/create_thumb_upload_02.php:
--------------------------------------------------------------------------------
1 | getMessages();
9 | } catch (Throwable $t) {
10 | echo $t->getMessage();
11 | }
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | Thumbnail Upload
19 |
20 |
21 |
22 | ';
25 | foreach ($messages as $message) {
26 | echo "$message ";
27 | }
28 | echo '';
29 | }
30 | ?>
31 |
32 |
33 | Upload images to create thumbnails:
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch11/Php8Solutions/Authenticate/CheckPassword_01.php:
--------------------------------------------------------------------------------
1 | minChars) || $this->minChars < self::MIN_LENGTH) {
17 | $this->minChars = self::MIN_LENGTH;
18 | }
19 | $this->check();
20 | }
21 |
22 | public function getErrors() {
23 | return $this->errors;
24 | }
25 |
26 | protected function check() {
27 | if (preg_match('/\s{2,}/', $this->password)) {
28 | $this->errors[] = 'Password can contain only single spaces.';
29 | }
30 | if (strlen($this->password) < $this->minChars) {
31 | $this->errors[] = "Password must be at least
32 | $this->minChars characters.";
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/ch11/authenticate.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Login
6 |
7 |
8 |
9 |
10 |
11 |
12 | Username:
13 |
14 |
15 |
16 | Password:
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/ch11/login_02.php:
--------------------------------------------------------------------------------
1 |
14 |
15 |
16 |
17 |
18 | Login
19 |
20 |
21 |
22 | $error";
25 | }
26 | ?>
27 |
28 |
29 | Username:
30 |
31 |
32 |
33 | Password:
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/ch11/login_03.php:
--------------------------------------------------------------------------------
1 |
14 |
15 |
16 |
17 |
18 | Login
19 |
20 |
21 |
22 | $error";
25 | } elseif (isset($_GET['expired'])) { ?>
26 | Your session has expired. Please log in again.
27 |
28 |
29 |
30 | Username:
31 |
32 |
33 |
34 | Password:
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch11/logout.php:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch11/menu_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Secret menu
6 |
7 |
8 |
9 | Restricted area
10 | Go to another secret page
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ch11/menu_02.php:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 | Secret menu
14 |
15 |
16 |
17 | Restricted area
18 | Go to another secret page
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch11/menu_03.php:
--------------------------------------------------------------------------------
1 |
22 |
23 |
24 |
25 |
26 | Secret menu
27 |
28 |
29 |
30 | Restricted area
31 | Go to another secret page
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ch11/menu_04.php:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 | Secret menu
15 |
16 |
17 |
18 | Restricted area
19 | Go to another secret page
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/ch11/menu_05.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Secret menu
7 |
8 |
9 |
10 | Restricted area
11 | Go to another secret page
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch11/multiple_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Multiple form 1
6 |
7 |
8 |
9 |
10 |
11 | First name:
12 |
13 |
14 |
15 | Family name:
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/ch11/multiple_01_done.php:
--------------------------------------------------------------------------------
1 |
14 |
15 |
16 |
17 |
18 | Multiple form 1
19 |
20 |
21 |
22 |
23 | Please fix the following required fields:
24 |
25 | $item";
28 | }
29 | ?>
30 |
31 |
32 |
33 |
34 | First name:
35 |
36 |
37 |
38 | Family name:
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/ch11/multiple_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Multiple form 2
6 |
7 |
8 |
9 |
10 |
11 | Age:
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch11/multiple_02_done.php:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
10 |
11 | Multiple form 2
12 |
13 |
14 |
15 |
16 | Please fix the following required fields:
17 |
18 | $item";
21 | }
22 | ?>
23 |
24 |
25 |
26 |
27 | Age:
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/ch11/multiple_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Multiple form 3
6 |
7 |
8 |
9 |
10 |
11 | Address:
12 |
13 |
14 |
15 | Town/City:
16 |
17 |
18 |
19 | Country:
20 |
21 | Please Select
22 | Canada
23 | France
24 | Germany
25 | Japan
26 | United Kingdom
27 | United States
28 | Other
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/ch11/multiple_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Multiple form 4
6 |
7 |
8 |
9 | The following details were submitted:
10 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/ch11/multiple_04_done.php:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 |
11 |
12 | Multiple form 4
13 |
14 |
15 |
16 | The following details were submitted:
17 |
18 | $key: " . htmlentities($_SESSION[$key] ) . '';
25 | // unset the session variable
26 | unset($_SESSION[$key]);
27 | }
28 | ?>
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/ch11/register_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Register User
6 |
17 |
18 |
19 |
20 | Register User
21 |
22 |
23 | Username:
24 |
25 |
26 |
27 | Password:
28 |
29 |
30 |
31 | Retype Password:
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/ch11/register_user_csv_01.php:
--------------------------------------------------------------------------------
1 | getErrors());
18 |
19 |
--------------------------------------------------------------------------------
/ch11/secretpage_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Secret page
6 |
7 |
8 |
9 | Restricted area
10 | Back to the secret menu
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ch11/secretpage_02.php:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 | Secret page
14 |
15 |
16 |
17 | Restricted area
18 | Back to the secret menu
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch11/secretpage_03.php:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 | Secret page
15 |
16 |
17 |
18 | Restricted area
19 | Back to the secret menu
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/ch11/secretpage_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Secret page
7 |
8 |
9 |
10 | Restricted area
11 | Back to the secret menu
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch11/session_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Session test
6 |
7 |
8 |
9 |
10 |
11 | Enter your name:
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch11/session_02.php:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 | Session test 2
15 |
16 |
17 |
18 | Next';
23 | } else {
24 | // if not set, send back to login
25 | echo 'Who are you? Please log in ';
26 | }
27 | ?>
28 |
29 |
30 |
--------------------------------------------------------------------------------
/ch11/session_03.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
8 | Session test 3
9 |
10 |
11 |
12 | ';
17 | // unset session variable
18 | unset($_SESSION['name']);
19 | // invalidate the session cookie
20 | if (isset($_COOKIE[session_name()])) {
21 | setcookie(session_name(), '', time()-86400, '/');
22 | }
23 | // end session
24 | session_destroy();
25 | echo 'Back to page 2 ';
26 | } else {
27 | // display if not recognized
28 | echo "Sorry, I don't know you. ";
29 | echo 'Please log in ';
30 | }
31 | ?>
32 |
33 |
34 |
--------------------------------------------------------------------------------
/ch11/session_04.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 | Session test 3
10 |
11 |
12 |
13 | ';
18 | // unset session variable
19 | unset($_SESSION['name']);
20 | // invalidate the session cookie
21 | if (isset($_COOKIE[session_name()])) {
22 | setcookie(session_name(), '', time()-86400, '/');
23 | }
24 | ob_end_flush();
25 | // end session
26 | session_destroy();
27 | echo 'Back to page 2 ';
28 | } else {
29 | // display if not recognized
30 | echo "Sorry, I don't know you. ";
31 | echo 'Please log in ';
32 | }
33 | ?>
34 |
35 |
36 |
--------------------------------------------------------------------------------
/ch11/session_timeout.php:
--------------------------------------------------------------------------------
1 | $_SESSION['start'] + $timelimit) {
15 | // if timelimit has expired, destroy session and redirect
16 | $_SESSION = [];
17 | // invalidate the session cookie
18 | if (isset($_COOKIE[session_name()])) {
19 | setcookie(session_name(), '', time()-86400, '/');
20 | }
21 | // end session and redirect with query string
22 | session_destroy();
23 | header("Location: {$redirect}?expired=yes");
24 | exit;
25 | } else {
26 | // if it's got this far, it's OK, so update start time
27 | $_SESSION['start'] = time();
28 | }
29 |
30 |
--------------------------------------------------------------------------------
/ch13/connection.php:
--------------------------------------------------------------------------------
1 | connect_error) {
17 | exit($conn->connect_error);
18 | }
19 | return $conn;
20 | } else {
21 | try {
22 | return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
23 | } catch (PDOException $e) {
24 | echo $e->getMessage();
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/ch13/connection_test.php:
--------------------------------------------------------------------------------
1 | query($sql);
6 | if (!$result) {
7 | $error = $conn->error;
8 | } else {
9 | $numRows = $result->num_rows;
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | MySQLi connection
17 |
18 |
19 |
20 | $error";
23 | } else {
24 | echo "A total of $numRows records were found.
";
25 | }
26 | ?>
27 |
28 |
29 |
--------------------------------------------------------------------------------
/ch13/mysqli_02.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | if (!$result) {
8 | $error = $conn->error;
9 | } else {
10 | $numRows = $result->num_rows;
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | MySQLi connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | fetch_assoc()) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/mysqli_03.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | if (!$result) {
8 | $error = $conn->error;
9 | } else {
10 | $numRows = $result->num_rows;
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | MySQLi connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | filename
30 | caption
31 |
32 | fetch_assoc()) { ?>
33 |
34 | = safe($row['filename']) ?>
35 | = safe($row['caption']) ?>
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/ch13/mysqli_04.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | if (!$result) {
8 | $error = $conn->error;
9 | } else {
10 | $numRows = $result->num_rows;
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | MySQLi connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | fetch_assoc()) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/mysqli_05.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | if (!$result) {
8 | $error = $conn->error;
9 | } else {
10 | $numRows = $result->num_rows;
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | MySQLi connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | fetch_assoc()) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/mysqli_06.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | if (!$result) {
9 | $error = $conn->error;
10 | } else {
11 | $numRows = $result->num_rows;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | MySQLi connection
19 |
20 |
21 |
22 | $error";
25 | } else {
26 | echo "A total of $numRows records were found.
";
27 | ?>
28 |
29 |
30 | image_id
31 | filename
32 | caption
33 |
34 | fetch_assoc()) { ?>
35 |
36 | = $row['image_id'] ?>
37 | = safe($row['filename']) ?>
38 | = safe($row['caption']) ?>
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch13/mysqli_07.php:
--------------------------------------------------------------------------------
1 | 5
7 | ORDER BY caption DESC';
8 | $result = $conn->query($sql);
9 | if (!$result) {
10 | $error = $conn->error;
11 | } else {
12 | $numRows = $result->num_rows;
13 | }
14 | ?>
15 |
16 |
17 |
18 |
19 | MySQLi connection
20 |
21 |
22 |
23 | $error";
26 | } else {
27 | echo "A total of $numRows records were found.
";
28 | ?>
29 |
30 |
31 | image_id
32 | filename
33 | caption
34 |
35 | fetch_assoc()) { ?>
36 |
37 | = $row['image_id'] ?>
38 | = safe($row['filename']) ?>
39 | = safe($row['caption']) ?>
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/ch13/mysqli_08.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | if (!$result) {
9 | $error = $conn->error;
10 | } else {
11 | $numRows = $result->num_rows;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | MySQLi connection
19 |
20 |
21 |
22 | $error";
25 | } else {
26 | echo "A total of $numRows records were found.
";
27 | ?>
28 |
29 |
30 | image_id
31 | filename
32 | caption
33 |
34 | fetch_assoc()) { ?>
35 |
36 | = $row['image_id'] ?>
37 | = safe($row['filename']) ?>
38 | = safe($row['caption']) ?>
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch13/mysqli_09.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | if (!$result) {
9 | $error = $conn->error;
10 | } else {
11 | $numRows = $result->num_rows;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | MySQLi connection
19 |
20 |
21 |
22 | $error";
25 | } else {
26 | echo "A total of $numRows records were found.
";
27 | ?>
28 |
29 |
30 | image_id
31 | filename
32 | caption
33 |
34 | fetch_assoc()) { ?>
35 |
36 | = $row['image_id'] ?>
37 | = safe($row['filename']) ?>
38 | = safe($row['caption']) ?>
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch13/mysqli_10.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | if (!$result) {
9 | $error = $conn->error;
10 | } else {
11 | $numRows = $result->num_rows;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | MySQLi connection
19 |
20 |
21 |
22 | $error";
25 | } else {
26 | echo "A total of $numRows records were found.
";
27 | ?>
28 |
29 |
30 | image_id
31 | filename
32 | caption
33 |
34 | fetch_assoc()) { ?>
35 |
36 | = $row['image_id'] ?>
37 | = safe($row['filename']) ?>
38 | = safe($row['caption']) ?>
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch13/mysqli_integer_01.php:
--------------------------------------------------------------------------------
1 | query($getImages);
7 | ?>
8 |
9 |
10 |
11 |
12 | MySQLi: Insert Integer in SQL
13 |
26 |
27 |
28 |
29 |
30 |
31 | fetch_assoc()) { ?>
32 |
36 | >= safe($row['filename']) ?>
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/ch13/mysqli_prepared_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | MySQLi Prepared Statement
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Number of results for :
14 |
15 |
16 | image_id
17 | filename
18 | caption
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch13/pdo_01.php:
--------------------------------------------------------------------------------
1 | query($sql);
6 | $error = $conn->errorInfo()[2];
7 | if (!$error) {
8 | $numRows = $result->rowCount();
9 | }
10 | ?>
11 |
12 |
13 |
14 |
15 | PDO connection
16 |
17 |
18 |
19 | $error";
22 | } else {
23 | echo "A total of $numRows records were found.
";
24 | }
25 | ?>
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ch13/pdo_02.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | $error = $conn->errorInfo()[2];
8 | if (!$error) {
9 | $numRows = $result->rowCount();
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | PDO connection
17 |
18 |
19 |
20 | $error";
23 | } else {
24 | echo "A total of $numRows records were found.
";
25 | ?>
26 |
27 |
28 | image_id
29 | filename
30 | caption
31 |
32 | query($sql) as $row) { ?>
33 |
34 | = $row['image_id'] ?>
35 | = safe($row['filename']) ?>
36 | = safe($row['caption']) ?>
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch13/pdo_03.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | $error = $conn->errorInfo()[2];
8 | if (!$error) {
9 | $numRows = $result->rowCount();
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | PDO connection
17 |
18 |
19 |
20 | $error";
23 | } else {
24 | echo "A total of $numRows records were found.
";
25 | ?>
26 |
27 |
28 | filename
29 | caption
30 |
31 | query($sql) as $row) { ?>
32 |
33 | = safe($row['filename']) ?>
34 | = safe($row['caption']) ?>
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch13/pdo_04.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | $error = $conn->errorInfo()[2];
8 | if (!$error) {
9 | $numRows = $result->rowCount();
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | PDO connection
17 |
18 |
19 |
20 | $error";
23 | } else {
24 | echo "A total of $numRows records were found.
";
25 | ?>
26 |
27 |
28 | image_id
29 | filename
30 | caption
31 |
32 | query($sql) as $row) { ?>
33 |
34 | = $row['image_id'] ?>
35 | = safe($row['filename']) ?>
36 | = safe($row['caption']) ?>
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch13/pdo_05.php:
--------------------------------------------------------------------------------
1 | query($sql);
7 | $error = $conn->errorInfo()[2];
8 | if (!$error) {
9 | $numRows = $result->rowCount();
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | PDO connection
17 |
18 |
19 |
20 | $error";
23 | } else {
24 | echo "A total of $numRows records were found.
";
25 | ?>
26 |
27 |
28 | image_id
29 | filename
30 | caption
31 |
32 | query($sql) as $row) { ?>
33 |
34 | = $row['image_id'] ?>
35 | = safe($row['filename']) ?>
36 | = safe($row['caption']) ?>
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/ch13/pdo_06.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | $error = $conn->errorInfo()[2];
9 | if (!$error) {
10 | $numRows = $result->rowCount();
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | PDO connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | query($sql) as $row) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/pdo_07.php:
--------------------------------------------------------------------------------
1 | 5
7 | ORDER BY caption DESC';
8 | $result = $conn->query($sql);
9 | $error = $conn->errorInfo()[2];
10 | if (!$error) {
11 | $numRows = $result->rowCount();
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | PDO connection
19 |
20 |
21 |
22 | $error";
25 | } else {
26 | echo "A total of $numRows records were found.
";
27 | ?>
28 |
29 |
30 | image_id
31 | filename
32 | caption
33 |
34 | query($sql) as $row) { ?>
35 |
36 | = $row['image_id'] ?>
37 | = safe($row['filename']) ?>
38 | = safe($row['caption']) ?>
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/ch13/pdo_08.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | $error = $conn->errorInfo()[2];
9 | if (!$error) {
10 | $numRows = $result->rowCount();
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | PDO connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | query($sql) as $row) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/pdo_09.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | $error = $conn->errorInfo()[2];
9 | if (!$error) {
10 | $numRows = $result->rowCount();
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | PDO connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | query($sql) as $row) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/pdo_10.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | $error = $conn->errorInfo()[2];
9 | if (!$error) {
10 | $numRows = $result->rowCount();
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | PDO connection
18 |
19 |
20 |
21 | $error";
24 | } else {
25 | echo "A total of $numRows records were found.
";
26 | ?>
27 |
28 |
29 | image_id
30 | filename
31 | caption
32 |
33 | query($sql) as $row) { ?>
34 |
35 | = $row['image_id'] ?>
36 | = safe($row['filename']) ?>
37 | = safe($row['caption']) ?>
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch13/pdo_integer_01.php:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
10 |
11 | PDO: Insert Integer in SQL
12 |
25 |
26 |
27 |
28 |
29 |
30 | query($getImages) as $row) { ?>
31 |
35 | >= safe($row['filename']) ?>
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch13/pdo_prepared_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | PDO Prepared Statement
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Number of results for :
14 |
15 |
16 | image_id
17 | filename
18 | caption
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch13/utility_funcs.php:
--------------------------------------------------------------------------------
1 | connect_error) {
17 | exit($conn->connect_error);
18 | }
19 | return $conn;
20 | } else {
21 | try {
22 | return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
23 | } catch (PDOException $e) {
24 | echo $e->getMessage();
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/ch14/footer.php:
--------------------------------------------------------------------------------
1 |
2 | ©
3 |
12 | David Powers
13 |
--------------------------------------------------------------------------------
/ch14/gallery_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Japan Journey = $title ?? ''; ?>
7 |
8 |
9 |
10 |
11 |
14 |
15 |
16 |
17 | Images of Japan
18 | Displaying 1 to 6 of 8
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | Water basin at Ryoanji temple, Kyoto
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/ch14/menu.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch14/title.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Insert Blog Entry
6 |
7 |
8 |
9 |
10 | Insert New Blog Entry
11 |
12 |
13 | Title:
14 |
15 |
16 |
17 | Article:
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/ch15/blog_insert_pdo_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Insert Blog Entry
6 |
7 |
8 |
9 |
10 | Insert New Blog Entry
11 |
12 |
13 | Title:
14 |
15 |
16 |
17 | Article:
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/ch15/blog_list_mysqli_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Manage Blog Entries
6 |
7 |
8 |
9 |
10 | Manage Blog Entries
11 | Insert new entry
12 |
13 |
14 | Created
15 | Title
16 |
17 |
18 |
19 |
20 |
21 |
22 | EDIT
23 | DELETE
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch15/blog_list_mysqli_02.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | if (!$result) {
9 | $error = $conn->error;
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | Manage Blog Entries
17 |
18 |
19 |
20 |
21 | Manage Blog Entries
22 | Insert new entry
23 | $error";
25 | } else { ?>
26 |
27 |
28 | Created
29 | Title
30 |
31 |
32 |
33 | fetch_assoc()) { ?>
34 |
35 | = $row['created'] ?>
36 | = safe($row['title']) ?>
37 | EDIT
38 | DELETE
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch15/blog_list_mysqli_03.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | if (!$result) {
9 | $error = $conn->error;
10 | }
11 | ?>
12 |
13 |
14 |
15 |
16 | Manage Blog Entries
17 |
18 |
19 |
20 |
21 | Manage Blog Entries
22 | Insert new entry
23 | $error";
25 | } else {
26 | if (isset($_GET['updated'])) {
27 | echo 'Record updated
';
28 | }
29 | ?>
30 |
31 |
32 | Created
33 | Title
34 |
35 |
36 |
37 | fetch_assoc()) { ?>
38 |
39 | = $row['created'] ?>
40 | = safe($row['title']) ?>
41 | EDIT
42 | DELETE
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/ch15/blog_list_pdo_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Manage Blog Entries
6 |
7 |
8 |
9 |
10 | Manage Blog Entries
11 | Insert new entry
12 |
13 |
14 | Created
15 | Title
16 |
17 |
18 |
19 |
20 |
21 |
22 | EDIT
23 | DELETE
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/ch15/blog_list_pdo_02.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | $error = $conn->errorInfo()[2];
9 | ?>
10 |
11 |
12 |
13 |
14 | Manage Blog Entries
15 |
16 |
17 |
18 |
19 | Manage Blog Entries
20 | Insert new entry
21 | $error";
23 | } else { ?>
24 |
25 |
26 | Created
27 | Title
28 |
29 |
30 |
31 | fetch()) { ?>
32 |
33 | = $row['created'] ?>
34 | = safe($row['title']) ?>
35 | EDIT
36 | DELETE
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/ch15/blog_list_pdo_03.php:
--------------------------------------------------------------------------------
1 | query($sql);
8 | $error = $conn->errorInfo()[2];
9 | ?>
10 |
11 |
12 |
13 |
14 | Manage Blog Entries
15 |
16 |
17 |
18 |
19 | Manage Blog Entries
20 | Insert new entry
21 | $error";
23 | } else {
24 | if (isset($_GET['updated'])) {
25 | echo 'Record updated
';
26 | }
27 | ?>
28 |
29 |
30 | Created
31 | Title
32 |
33 |
34 |
35 | fetch()) { ?>
36 |
37 | = $row['created'] ?>
38 | = safe($row['title']) ?>
39 | EDIT
40 | DELETE
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/ch15/blog_update_mysqli_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Update Blog Entry
6 |
7 |
8 |
9 |
10 | Update Blog Entry
11 | List all entries
12 |
13 |
14 | Title:
15 |
16 |
17 |
18 | Article:
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ch15/blog_update_pdo_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Update Blog Entry
6 |
7 |
8 |
9 |
10 | Update Blog Entry
11 | List all entries
12 |
13 |
14 | Title:
15 |
16 |
17 |
18 | Article:
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/ch15/connection.php:
--------------------------------------------------------------------------------
1 | connect_error) {
17 | exit($conn->connect_error);
18 | }
19 | return $conn;
20 | } else {
21 | try {
22 | return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
23 | } catch (PDOException $e) {
24 | echo $e->getMessage();
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/ch16/blog_left_mysqli.php:
--------------------------------------------------------------------------------
1 | query($sql);
10 | if (!$result) {
11 | $error = $conn->error;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | Japan Journey = $title ?? ''; ?>
19 |
20 |
21 |
22 |
23 |
26 |
27 |
28 |
29 | $error";
32 | } else {
33 | while ($row = $result->fetch_assoc()) { ?>
34 | = safe($row['title']) ?>
35 | = safe($row['first100']) ?>
36 | More
37 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch16/blog_left_pdo.php:
--------------------------------------------------------------------------------
1 | query($sql);
10 | $error = $conn->errorInfo()[2];
11 | ?>
12 |
13 |
14 |
15 |
16 | Japan Journey = $title ?? ''; ?>
17 |
18 |
19 |
20 |
21 |
24 |
25 |
26 |
27 | $error";
30 | } else {
31 | while ($row = $result->fetch()) { ?>
32 | = safe($row['title']) ?>
33 | = safe($row['first100']) ?>
34 | More
35 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/ch16/blog_mysqli.php:
--------------------------------------------------------------------------------
1 | query($sql);
9 | if (!$result) {
10 | $error = $conn->error;
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 | Japan Journey
18 |
19 |
20 |
21 |
22 |
25 |
26 |
27 |
28 | $error";
30 | } else {
31 | while ($row = $result->fetch_assoc()) {
32 | echo "{$row['title']} ";
33 | $extract = getFirst($row['article']);
34 | echo '' . safe($extract[0]);
35 | if ($extract[1]) {
36 | echo '
37 | More ';
38 | }
39 | echo '
';
40 | }
41 |
42 | }
43 | ?>
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/ch16/blog_para_mysqli.php:
--------------------------------------------------------------------------------
1 | query($sql);
10 | if (!$result) {
11 | $error = $conn->error;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | Japan Journey
19 |
20 |
21 |
22 |
23 |
26 |
27 |
28 |
29 | $error";
32 | } else {
33 | while ($row = $result->fetch_assoc()) { ?>
34 | = safe($row['title']) ?>
35 | = safe(substr($row['article'], 0, strpos($row['article'], PHP_EOL))) ?>
36 | More
37 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/ch16/blog_para_pdo.php:
--------------------------------------------------------------------------------
1 | query($sql);
9 | $error = $conn->errorInfo()[2];
10 | ?>
11 |
12 |
13 |
14 |
15 | Japan Journey = $title ?? ''; ?>
16 |
17 |
18 |
19 |
20 |
23 |
24 |
25 |
26 | $error";
29 | } else {
30 | while ($row = $result->fetch()) { ?>
31 | = $row['title']; ?>
32 | = substr($row['article'], 0, strpos($row['article'], PHP_EOL)); ?>
33 | More
34 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch16/blog_ptags_mysqli.php:
--------------------------------------------------------------------------------
1 | query($sql);
10 | if (!$result) {
11 | $error = $conn->error;
12 | }
13 | ?>
14 |
15 |
16 |
17 |
18 | Japan Journey = $title ?? ''; ?>
19 |
20 |
21 |
22 |
23 |
26 |
27 |
28 |
29 | $error";
32 | } else {
33 | while ($row = $result->fetch_assoc()) { ?>
34 | = $row['title']; ?>
35 | = convertToParas($row['article']); ?>
36 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/ch16/blog_ptags_pdo.php:
--------------------------------------------------------------------------------
1 | query($sql);
10 | $error = $conn->errorInfo()[2];
11 | ?>
12 |
13 |
14 |
15 |
16 | Japan Journey = $title ?? ''; ?>
17 |
18 |
19 |
20 |
21 |
24 |
25 |
26 |
27 | $error";
30 | } else {
31 | while ($row = $result->fetch()) { ?>
32 | = $row['title']; ?>
33 | = convertToParas($row['article']); ?>
34 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/ch16/connection.php:
--------------------------------------------------------------------------------
1 | connect_error) {
17 | exit($conn->connect_error);
18 | }
19 | return $conn;
20 | } else {
21 | try {
22 | return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
23 | } catch (PDOException $e) {
24 | echo $e->getMessage();
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/ch16/date_converter_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Convert Date to ISO Format
7 |
12 |
13 |
14 |
15 |
16 |
17 | Month:
18 |
19 |
20 |
21 | Date:
22 |
23 | Year:
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/ch16/date_format_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Formatting DateTime Objects
6 |
7 |
8 |
9 |
13 | It's now = $now->format('g.ia') ?> on = $now->format('l, F jS, Y') ?>
14 | Christmas 2021 falls on a = $xmas2021->format('l') ?>
15 |
16 |
--------------------------------------------------------------------------------
/ch16/date_format_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Formatting with date()
6 |
7 |
8 |
9 |
10 | It's now = date('g.ia') ?> on = date('l, F jS, Y') ?>
11 | Christmas 2021 falls on a = date('l', $xmas2021) ?>
12 |
13 |
--------------------------------------------------------------------------------
/ch16/date_format_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Formatting with date()
6 |
7 |
8 |
9 | It's now = date('g.ia') ?> on = date('l, F jS, Y') ?>
10 | Christmas 2021 falls on a = date('l', strtotime('12/25/2021')) ?>
11 |
12 |
--------------------------------------------------------------------------------
/ch16/date_format_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DateTime::createFromFormat
6 |
7 |
8 |
9 | format('l, jS F Y');
12 | ?>
13 |
14 |
--------------------------------------------------------------------------------
/ch16/date_format_05.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DateTime v date()
6 |
7 |
8 |
9 | format('g.ia') . ' ';
13 |
14 | // Using the date() function produces the same output for less effort
15 | echo "It's now " . date('g.ia');
16 | ?>
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch16/date_interval_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DateInterval: Add
6 |
7 |
8 |
9 | add(new DateInterval('P12D'));
12 | ?>
13 | Twelfth Night falls on = $xmas2021->format('l, F jS, Y'); ?>.
14 |
15 |
--------------------------------------------------------------------------------
/ch16/date_interval_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DateInterval: Add
6 |
7 |
8 |
9 | add(DateInterval::createFromDateString('+12 days'));
12 | ?>
13 | Twelfth Night falls on = $xmas2021->format('l, F jS, Y'); ?>.
14 |
15 |
--------------------------------------------------------------------------------
/ch16/date_interval_03.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DateInterval: Diff
6 |
7 |
8 |
9 | diff($independence);
13 | echo $interval->format('%Y years %m months %d days'); ?> since American Declaration of Independence.
14 |
15 |
--------------------------------------------------------------------------------
/ch16/date_interval_04.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DatePeriod: Recurring Dates
6 |
20 |
21 |
22 |
23 | Second Tuesday of Each Month in 2022
24 |
25 | format('l, F jS, Y') . ' ';
31 | }
32 | ?>
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ch16/date_interval_05.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DatePeriod: Recurring Dates
6 |
20 |
21 |
22 |
23 | Second Tuesday of Each Month in 2022
24 |
25 | format('l, F jS, Y') . ' ';
32 | }
33 | ?>
34 |
35 |
36 |
--------------------------------------------------------------------------------
/ch16/date_interval_06.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | DatePeriod: ISO Recurring Dates
6 |
7 |
8 |
9 |
10 | format('l, F j, Y') . ' ';
14 | }
15 | ?>
16 |
17 |
18 |
--------------------------------------------------------------------------------
/ch16/date_modify_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Modify Dates
6 |
7 |
8 |
9 |
13 | Original date: = $date->format($format) ?>.
14 | Add one month: modify('+1 month');
16 | echo $date->format($format);
17 | $date->modify('-1 month');
18 | ?>
19 |
Subtract one month: = $date->format($format) ?>
20 |
21 |
--------------------------------------------------------------------------------
/ch16/date_modify_02.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Modify Dates
6 |
7 |
8 |
9 |
13 | Original date: = $date->format($format) ?>.
14 | Add one month: modify('last day of +1 month');
16 | echo $date->format($format);
17 | $date->modify('last day of -1 month');
18 | ?>
19 |
Subtract one month: = $date->format($format) ?>
20 |
21 |
--------------------------------------------------------------------------------
/ch16/date_test.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Date Picker Test
6 |
7 |
8 |
9 |
10 |
11 |
12 | Date:
13 |
14 |
15 |
16 |
17 |
18 |
19 |
24 |
25 |
--------------------------------------------------------------------------------
/ch16/footer.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/ch16/menu.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
--------------------------------------------------------------------------------
/ch16/setDate.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Last Day of Month
6 |
7 |
8 |
9 | setDate(2022, 3, 0);
13 | ?>
14 | Non-leap year: = $date->format($format) ?>.
15 | Leap year: setDate(2024, 3, 0);
16 | echo $date->format($format); ?>.
17 |
18 |
--------------------------------------------------------------------------------
/ch16/timezone_display.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Get Server's Time Zone
6 |
7 |
8 |
9 | The server's time zone is
10 | getTimezone();
13 | echo $timezone->getName();
14 | ?>
15 | .
16 |
17 |
--------------------------------------------------------------------------------
/ch16/timezones.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Changing Time Zones
6 |
7 |
8 |
9 |
15 | In London, it's now = $now->format('l, F jS, Y g.ia'); ?>.
16 | In New York, it's setTimezone($USeast);
18 | echo $now->format('l, F jS, Y g.ia'); ?>.
19 | In Hawaii, it's setTimezone($Hawaii);
21 | echo $now->format('l, F jS, Y g.ia'); ?>.
22 |
23 |
--------------------------------------------------------------------------------
/ch16/title.php:
--------------------------------------------------------------------------------
1 | connect_error) {
17 | exit($conn->connect_error);
18 | }
19 | return $conn;
20 | } else {
21 | try {
22 | return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
23 | } catch (PDOException $e) {
24 | echo $e->getMessage();
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/ch17/details_01.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Japan Journey
6 |
7 |
8 |
9 |
10 |
13 |
14 |
15 |
16 | Article Title Goes Here
17 | Saturday, November 2nd, 2019
18 |
19 |
20 |
21 | Article contents go here.
22 | Back to the blog
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ch17/footer.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/ch17/menu.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch17/test.php:
--------------------------------------------------------------------------------
1 | query($sql);
9 | if (!$result) {
10 | $error = $conn->error;
11 | }
12 | ?>
13 |
14 |
15 |
16 |
17 |
18 | Check Storage Engines
19 |
20 |
21 |
22 | $error";
24 | } else { ?>
25 |
26 |
27 | Storage Engine Supported
28 |
29 | fetch_assoc()) { ?>
30 |
31 | = $row['Engine']; ?> = $row['Support']; ?>
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/ch18/toggle_fields.js:
--------------------------------------------------------------------------------
1 | const cbox = document.getElementById('allowUpload');
2 | cbox.style.display = 'block';
3 | var uploadImage = document.getElementById('upload_new');
4 | uploadImage.onclick = function () {
5 | const image_id = document.getElementById('image_id');
6 | const image = document.getElementById('image');
7 | const caption = document.getElementById('caption');
8 | const sel = uploadImage.checked;
9 | image_id.disabled = sel;
10 | image.parentNode.style.display = sel ? 'block' : 'none';
11 | caption.parentNode.style.display = sel ? 'block' : 'none';
12 | image.disabled = !sel;
13 | caption.disabled = !sel;
14 | }
15 |
--------------------------------------------------------------------------------
/ch19/authenticate_2way_mysqli.php:
--------------------------------------------------------------------------------
1 | stmt_init();
10 | $stmt->prepare($sql);
11 | // bind the input parameters
12 | $stmt->bind_param('sss', $username, $password, $key);
13 | $stmt->execute();
14 | // to get the number of matches, you must store the result
15 | $stmt->store_result();
16 | // if a match is found, num_rows is 1, which is treated as true
17 | if ($stmt->num_rows) {
18 | $_SESSION['authenticated'] = 'Jethro Tull';
19 | // get the time the session started
20 | $_SESSION['start'] = time();
21 | session_regenerate_id();
22 | header("Location: $redirect"); exit;
23 | } else {
24 | // if not verified, prepare error message
25 | $error = 'Invalid username or password';
26 | }
--------------------------------------------------------------------------------
/ch19/authenticate_2way_pdo.php:
--------------------------------------------------------------------------------
1 | prepare($sql);
10 | // bind variables by passing them as an array when executing statement
11 | $stmt->execute([$username, $password, $key]);
12 | // if a match is found, rowCount() produces 1, which is treated as true
13 | if ($stmt->rowCount()) {
14 | $_SESSION['authenticated'] = 'Jethro Tull';
15 | // get the time the session started
16 | $_SESSION['start'] = time();
17 | session_regenerate_id();
18 | header("Location: $redirect"); exit;
19 | } else {
20 | // if not verified, prepare error message
21 | $error = 'Invalid username or password';
22 | }
--------------------------------------------------------------------------------
/ch19/authenticate_mysqli.php:
--------------------------------------------------------------------------------
1 | stmt_init();
8 | $stmt->prepare($sql);
9 | // bind the input parameter
10 | $stmt->bind_param('s', $username);
11 | $stmt->execute();
12 | // bind the result, using a new variable for the password
13 | $stmt->bind_result($storedPwd);
14 | $stmt->fetch();
15 | // check the submitted password against the stored version
16 | if (password_verify($password, $storedPwd)) {
17 | $_SESSION['authenticated'] = 'Jethro Tull';
18 | // get the time the session started
19 | $_SESSION['start'] = time();
20 | session_regenerate_id();
21 | header("Location: $redirect");
22 | exit;
23 | } else {
24 | // if not verified, prepare error message
25 | $error = 'Invalid username or password';
26 | }
--------------------------------------------------------------------------------
/ch19/authenticate_pdo.php:
--------------------------------------------------------------------------------
1 | prepare($sql);
8 | // pass the input parameter as a single-element array
9 | $stmt->execute([$username]);
10 | $storedPwd = $stmt->fetchColumn();
11 | // check the submitted password against the stored version
12 | if (password_verify($password, $storedPwd)) {
13 | $_SESSION['authenticated'] = 'Jethro Tull';
14 | // get the time the session started
15 | $_SESSION['start'] = time();
16 | session_regenerate_id();
17 | header("Location: $redirect");
18 | exit;
19 | } else {
20 | // if not verified, prepare error message
21 | $error = 'Invalid username or password';
22 | }
--------------------------------------------------------------------------------
/ch19/login_db.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Login
6 |
7 |
8 |
9 | $error";
12 | } elseif (isset($_GET['expired'])) { ?>
13 | Your session has expired. Please log in again.
14 |
15 |
16 |
17 | Username:
18 |
19 |
20 |
21 | Password:
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/ch19/login_mysqli.php:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
15 |
16 | Login
17 |
18 |
19 |
20 | $error";
23 | } elseif (isset($_GET['expired'])) { ?>
24 | Your session has expired. Please log in again.
25 |
26 |
27 |
28 | Username:
29 |
30 |
31 |
32 | Password:
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch19/login_pdo.php:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
15 |
16 | Login
17 |
18 |
19 |
20 | $error";
23 | } elseif (isset($_GET['expired'])) { ?>
24 | Your session has expired. Please log in again.
25 |
26 |
27 |
28 | Username:
29 |
30 |
31 |
32 | Password:
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/ch19/logout_db.php:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch19/menu_db.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Secret menu
7 |
8 |
9 |
10 | Restricted area
11 | Go to another secret page
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch19/register_db.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Register user
6 |
17 |
18 |
19 |
20 | Register User
21 |
22 |
23 | Username:
24 |
25 |
26 |
27 | Password:
28 |
29 |
30 |
31 | Retype Password:
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/ch19/secretpage_db.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Secret page
7 |
8 |
9 |
10 | Restricted area
11 | Back to the secret menu
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ch19/session_timeout_db.php:
--------------------------------------------------------------------------------
1 | $_SESSION['start'] + $timelimit) {
15 | // if timelimit has expired, destroy session and redirect
16 | $_SESSION = [];
17 | // invalidate the session cookie
18 | if (isset($_COOKIE[session_name()])) {
19 | setcookie(session_name(), '', time()-86400, '/');
20 | }
21 | // end session and redirect with query string
22 | session_destroy();
23 | header("Location: {$redirect}?expired=yes");
24 | exit;
25 | } else {
26 | // if it's got this far, it's OK, so update start time
27 | $_SESSION['start'] = time();
28 | }
29 |
--------------------------------------------------------------------------------
/errata.md:
--------------------------------------------------------------------------------
1 | # Errata for *PHP 8 Solutions*
2 |
3 | On **page xx** [Summary of error]:
4 |
5 | Details of error here. Highlight key pieces in **bold**.
6 |
7 | ***
8 |
9 | On **page xx** [Summary of error]:
10 |
11 | Details of error here. Highlight key pieces in **bold**.
12 |
13 | ***
--------------------------------------------------------------------------------
/images/basin.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/basin.jpg
--------------------------------------------------------------------------------
/images/fountains.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/fountains.jpg
--------------------------------------------------------------------------------
/images/fuji.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/fuji.jpg
--------------------------------------------------------------------------------
/images/kinkakuji.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/kinkakuji.jpg
--------------------------------------------------------------------------------
/images/maiko.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/maiko.jpg
--------------------------------------------------------------------------------
/images/maiko_phone.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/maiko_phone.jpg
--------------------------------------------------------------------------------
/images/menu.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/menu.jpg
--------------------------------------------------------------------------------
/images/monk.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/monk.jpg
--------------------------------------------------------------------------------
/images/ryoanji.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/ryoanji.jpg
--------------------------------------------------------------------------------
/images/thumbs/basin.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/basin.jpg
--------------------------------------------------------------------------------
/images/thumbs/fountains.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/fountains.jpg
--------------------------------------------------------------------------------
/images/thumbs/kinkakuji.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/kinkakuji.jpg
--------------------------------------------------------------------------------
/images/thumbs/maiko.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/maiko.jpg
--------------------------------------------------------------------------------
/images/thumbs/maiko_phone.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/maiko_phone.jpg
--------------------------------------------------------------------------------
/images/thumbs/menu.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/menu.jpg
--------------------------------------------------------------------------------
/images/thumbs/monk.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/monk.jpg
--------------------------------------------------------------------------------
/images/thumbs/ryoanji.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Apress/php-8-solutions/df044e148a2e5618e5e93e8716bbca0045bd4809/images/thumbs/ryoanji.jpg
--------------------------------------------------------------------------------
/styles/admin.css:
--------------------------------------------------------------------------------
1 | /* CSS Document */
2 |
3 | body {
4 | margin:25px;
5 | color:#000;
6 | background-color: #fff;
7 | font-family:Arial, Helvetica, sans-serif;
8 | font-size:85%;
9 | }
10 | h1 {
11 | font-size: 140%;
12 | }
13 | form {
14 | margin-left: 25px;
15 | }
16 | label {
17 | font-weight: bold;
18 | display:block;
19 | }
20 | .warning {
21 | color:#f00;
22 | font-weight:bold;
23 | }
24 | input[type="text"], textarea {
25 | width: 500px;
26 | }
27 | textarea {
28 | height: 125px;
29 | }
30 | td {
31 | padding: 2px 5px 2px 10px;
32 | }
33 | #allowUpload, .optional {
34 | display:none;
35 | }
36 |
--------------------------------------------------------------------------------
/styles/newsfeed.css:
--------------------------------------------------------------------------------
1 | @charset "utf-8";
2 | /* CSS Document */
3 |
4 | body {
5 | background-color:#fff;
6 | color:#000;
7 | font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
8 | }
9 | h1 {
10 | font-size:150%;
11 | margin-left:20px;
12 | color:#A51306;
13 | }
14 | h2 {
15 | font-size:100%;
16 | margin-left:50px;
17 | margin-bottom:3px;
18 | }
19 | a {
20 | color:#1F4F82;
21 | }
22 | a:link, a:visited {
23 | text-decoration:none;
24 | }
25 | a:hover, a:active, a:focus {
26 | text-decoration:underline;
27 | }
28 | .datetime {
29 | font-size: 70%;
30 | margin: 0 auto 5px 80px;
31 | }
32 | p {
33 | font-size:90%;
34 | margin:5px auto 10px 50px;
35 | width:400px;
36 | }
--------------------------------------------------------------------------------