{"id":26871257,"url":"https://github.com/codeadamca/php-for-while","last_synced_at":"2026-02-25T22:04:34.303Z","repository":{"id":115328429,"uuid":"296190237","full_name":"codeadamca/php-for-while","owner":"codeadamca","description":"A basic example of using PHP for and while loops. ","archived":false,"fork":false,"pushed_at":"2025-01-26T22:01:14.000Z","size":293,"stargazers_count":0,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T06:38:14.294Z","etag":null,"topics":["for-loop","learning-code","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-17T01:47:23.000Z","updated_at":"2025-01-26T22:01:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"d3775868-482d-4ac3-b5dc-b917391d98cf","html_url":"https://github.com/codeadamca/php-for-while","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/codeadamca/php-for-while","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-for-while","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-for-while/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-for-while/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-for-while/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/php-for-while/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-for-while/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29842894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T21:18:31.832Z","status":"ssl_error","status_checked_at":"2026-02-25T21:18:29.265Z","response_time":61,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["for-loop","learning-code","php"],"created_at":"2025-03-31T07:18:54.789Z","updated_at":"2026-02-25T22:04:34.287Z","avatar_url":"https://github.com/codeadamca.png","language":"PHP","readme":"# A Basic Introduction to PHP and For Loops\n\nA basic example of using PHP ifor and while loops.\n\nThis tutorial will review how to use for and while loops to repeat blocks of code. Here is a basic sample of using a for loop to repeat a block of code.\n\n```php\n\u003c?php \n\nfor ($i = 0; $i \u003c 10; $i++)\n{\n  echo '\u003cp\u003eThis is loop number '.$i.'!\u003c/p\u003e';\n}\n\n?\u003e\n```\n\nThis is an example of using a whle to achieve the same result.\n\n```php\n\u003c?php\n\n$i = 0;\n\nwhile ($i \u003c 10)\n{\n  echo '\u003cp\u003eThis is loop number '.$i.'!\u003c/p\u003e';\n  $i ++;\n}\n\n?\u003e\n```\n\n## The End Goal\n\nThe `for.php` file includes an array with data for three links (Codecademy, W3Schools, and MDN). The array closely resembles data from a MySQL database. \n\nSimilar to the variables, arrays, and if examples, your code needs to output the four values. However, this time it will display all three links at the same time. Your code will need to loop through the three links and then output the link information using the arrays and if statements.\n\nFor example the following code loops through an array and outputs the values.\n\n```php\n\u003c?php\n\n$data = array (\n  array (\n    'name' =\u003e'PHP: Hypertext Preprocessor',\n    'short' =\u003e 'PHP'\n  ),\n  array (\n    'name' =\u003e 'Hyper Text Markup Language',\n    'short' =\u003e 'HTML'\n  ),\n  array (\n    'name' =\u003e 'Cascading Stylesheets',\n    'short' =\u003e 'CSS'\n  )\n);\n\nfor ($i = 0; $i \u003c count($data); $i ++)\n{\n  echo '\u003cp\u003e'.$data[$i]['name'].' ('.$data[$i]['short'].')\u003c/p\u003e';\n}\n\n?\u003e\n```\n\nIf a piece of data is missing, try to think of an alternative. For example if a title is missing, we could use the URL as the title. It's not as good as a title, but it's better than nothing. \n\n## Steps\n\n1. Open up a new file and name it `for.php`.\n2. Add the following code to the new PHP file:\n    \n    ```php\n    \u003c!doctype html\u003e\n    \u003chtml\u003e\n    \u003chead\u003e\n       \u003ctitle\u003ePHP and For Loops\u003c/title\u003e \n    \u003c/head\u003e\n    \u003cbody\u003e\n        \n      \u003ch1\u003ePHP and For Loops\u003c/h1\u003e \n    \n      \u003cp\u003eUse PHP echo, if statements, and loops to output all three links.\u003c/p\u003e\n    \n      \u003c?php\n    \n      // **************************************************\n      // Do not edit this code\n      \n      $links = array (\n        0 =\u003e array (\n          'name' =\u003e 'Codecademy',\n          'url' =\u003e'https://www.codecademy.com/',\n          'image' =\u003e '',\n          'description' =\u003e 'Learn to code interactively, for free.' ),\n        1 =\u003e array ( \n          'name' =\u003e '',\n          'url' =\u003e 'https://www.w3schools.com/',\n          'image' =\u003e 'w3schools.png',\n          'description' =\u003e 'W3Schools is optimized for learning, testing, and training.' ),\n        2 =\u003e array (\n          'name' =\u003e 'Mozilla Developer Network',\n          'url' =\u003e 'https://www.codecademy.com/',\n          'image' =\u003e 'mozilla.png',\n          'description' =\u003e 'The Mozilla Developer Network (MDN) provides information about Open Web technologies.' )\n        );\n        \n      // **************************************************\n    \n      for ($i = 0; $i \u003c count ($links); $i ++)\n      {\n        echo '\u003ch1\u003e'.$links[$i]['name'].'\u003c/h1\u003e';\n      }\n    \n      echo '\u003cpre\u003e';\n      print_r ($links);\n      echo '\u003c/pre\u003e';\n    \n      ?\u003e\n        \n    \u003c/body\u003e\n    \u003c/html\u003e\n    \n    ```\n    \n    \u003e Note: Do not edit the code between the stars. \n\n3. After the stars, add code that will display the three sets of link information using loops. Notice that some of the values are purposely empty. \n\n    \u003e Hint: Add each value from the arrays one at a time. Test your PHP after each new line of PHP. \n\n\u003e [More information on PHP for loops](https://www.php.net/manual/en/control-structures.for.php)\n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/php-for-while.html\n\n***\n\n## Repo Resources\n\n* [Visual Studio Code](https://code.visualstudio.com/)\n* [Filezilla](https://filezilla-project.org/) (or any FTP program)\n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-for-while","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fphp-for-while","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-for-while/lists"}