{"id":26871262,"url":"https://github.com/codeadamca/php-if-switch","last_synced_at":"2025-03-31T07:18:57.181Z","repository":{"id":115328474,"uuid":"296178327","full_name":"codeadamca/php-if-switch","owner":"codeadamca","description":"A basic example of using PHP to make decisions.","archived":false,"fork":false,"pushed_at":"2025-01-26T21:40:09.000Z","size":304,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-26T22:25:51.476Z","etag":null,"topics":["if-statements","learning-code","php","switch-statements"],"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}},"created_at":"2020-09-17T00:32:24.000Z","updated_at":"2025-01-26T21:40:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"f0a369b7-e583-4335-8f14-a59195594e08","html_url":"https://github.com/codeadamca/php-if-switch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-if-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-if-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-if-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-if-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/php-if-switch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246429486,"owners_count":20775809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["if-statements","learning-code","php","switch-statements"],"created_at":"2025-03-31T07:18:56.486Z","updated_at":"2025-03-31T07:18:57.163Z","avatar_url":"https://github.com/codeadamca.png","language":"PHP","readme":"# A Basic Introduction to PHP and If Statements\n\nA basic example of using PHP if statements and switches.\n\nThis tutorial will review how to use if statements and switches to make decisions in PHP. Here is a basic sample of using an if statements to amke a decision.\n\n```php\n\u003c?php \n\n$hour = date('24');\n\nif ($hour \u003c 12)\n{\n  echo 'Good morning!';\n }\n else\n {\n   echo 'Good afternoon!';\n }\n\n?\u003e\n```\n\nThis is an example of using a switch.\n\n```php\n\u003c?php\n\n$day = date('D');\n\nswitch ($day) \n{\n  case \"Monday\":\n    echo \"Sounds like someone has a case of the Mondays!\";\n    break;\n  case \"Friday\":\n    echo \"TFIG!\";\n    break;\n  default:\n    echo \"Have a great day!\";\n    break;\n}\n\n?\u003e\n```\n\n## The End Goal\n\nThe `if.php` file includes three copies of four different varables (Codecademy, W3Schools, and MDN). The code will chose a random number (1, 2, or 3) and then chose a set of variables: Codecademy, W3Schools, or MDN. \n\nSimilar to the variables and arrays examples, your code needs to output the four values. However, each time you refresh the page the data will change. Depending on the chosen random number some of the variables may be missing data. When you output the link data your PHP will need to confirm that the data exists before it outputs it. \n\nFor example the following code checks to see if `$linkName` variable has a value before it outputs it.\n\n```php\n\u003c?php\n\nif ($linkName != \"\") \n{\n  echo '\u003ch2\u003e'.$linkName.'\u003c/h2\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 `if.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 If Statements\u003c/title\u003e \n      \u003c/head\u003e\n      \u003cbody\u003e\n    \n        \u003ch1\u003ePHP If Statements\u003c/h1\u003e \n    \n        \u003cp\u003eUse PHP echo and variables to output the following link information, use if statements to make sure everything outputs correctly:\u003c/p\u003e\n    \n        \u003c?php\n    \n        // **************************************************\n        // Do not edit this code\n    \n        $randomNumber = ceil(rand(1,3));\n    \n        echo '\u003cp\u003eThe random number is '.$randomNumber.'.\u003c/p\u003e';\n    \n        if ($randomNumber == 1)\n        {\n          $linkName = 'Codecademy';\n          $linkURL = 'https://www.codecademy.com/';\n          $linkImage = '';\n          $linkDescription = 'Learn to code interactively, for free.';\n        }\n        elseif ($randomNumber == 2)\n        {\n          $linkName = '';\n          $linkURL = 'https://www.w3schools.com/';\n          $linkImage = 'w3schools.png';\n          $linkDescription = 'W3Schools is optimized for learning, testing, and training.';\n        }\n        else\n        {\n          $linkName = 'Mozilla Developer Network';\n          $linkURL = 'https://developer.mozilla.org';\n          $linkImage = 'mozilla.png';\n          $linkDescription = 'The Mozilla Developer Network (MDN) provides information about Open Web technologies.';\n        }\n    \n        // **************************************************\n    \n        echo '\u003ch2\u003e'.$linkName.'\u003c/h2\u003e';\n    \n        ?\u003e\n    \n      \u003c/body\u003e\n    \u003c/html\u003e\n    ```\n\n\u003e [!Note]\n\u003e Do not edit the code between the stars. \n\n3. After the stars, add code that will display the four chosen variables using if statements to confirm that the data exists. Notice that some of the variables are purposely empty. \n\n\u003e [!Note]\n\u003e Add each value from the variables one at a time. Test your PHP after each new line of PHP. \n\n\u003e [More information on PHP if statements](https://www.php.net/manual/en/control-structures.if.php)\n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/php-if-switch.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-if-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fphp-if-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-if-switch/lists"}