Difference between revisions of "3811 Introduction To PHP"

From Coder Merlin
m (NerdOfLinux moved page 3811 php Hello World to 3811 Introduction To PHP: Improve navigation)
Line 1: Line 1:
== Prerequisites ==
== Prerequisites ==
In order to get started with PHP on the CoderMerlin server, you'll first need to make sure you have your '''www''' folder set up properly. This should be in your home directory; if it does not exist, then you can create the folder by using the '''mkdir''' command:
<syntaxhighlight lang="bash">
mkdir ~/www
</syntaxhighlight>
Once you have created the folder (or if it already exists), you'll need to set up the permissions for the folder. This isn't as hard as it sounds; simply run the following command:
<syntaxhighlight lang="bash">
chmod a+rx ~/www
</syntaxhighlight>
If you ever encounter any "Access denied." error messages, then run the following command, replacing file.php with the full path and filename (e.g. ~/www/index.php):
<syntaxhighlight lang="bash">
chmod a+rx file.php
</syntaxhighlight>
== Hello World ==
For the first example, we'll simply print "Hello, World!" in your browser. This will help test if everything is set up properly, and is quite simple. Simply create a file called index.php in your www folder with the following contents:
<syntaxhighlight lang="php">
<?php
echo 'Hello, World!';
</syntaxhighlight>
Once you save the file, you can visit it using your own CoderMerlin URL which takes the form of https://www.codermerlin.com/users/your-user-name/filename
In this case, the filename is simply ''index.php'' because we placed the file directly in your www folder as opposed to in a subfolder.
=== Troubleshooting ===
* '''Access denied''': make sure you have set the permissions properly (see the previous section)
* '''Not found''': be sure to double check your URL
* '''500 or server error''': be sure to verify that you have copied the entire contents of the index.php file; even a single missing semicolon or opening PHP tag can cause this error
If the issue persists, please follow the instructions in [[W1003_Help_Me!]]
== Background ==
== Background ==
[https://www.w3schools.com/php/ PHP] (w3 Schools)
[https://www.w3schools.com/php/ PHP] (w3 Schools)

Revision as of 20:10, 2 January 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Prerequisites[edit]

In order to get started with PHP on the CoderMerlin server, you'll first need to make sure you have your www folder set up properly. This should be in your home directory; if it does not exist, then you can create the folder by using the mkdir command:

mkdir ~/www

Once you have created the folder (or if it already exists), you'll need to set up the permissions for the folder. This isn't as hard as it sounds; simply run the following command:

chmod a+rx ~/www

If you ever encounter any "Access denied." error messages, then run the following command, replacing file.php with the full path and filename (e.g. ~/www/index.php):

chmod a+rx file.php

Hello World[edit]

For the first example, we'll simply print "Hello, World!" in your browser. This will help test if everything is set up properly, and is quite simple. Simply create a file called index.php in your www folder with the following contents:

<?php
echo 'Hello, World!';

Once you save the file, you can visit it using your own CoderMerlin URL which takes the form of https://www.codermerlin.com/users/your-user-name/filename

In this case, the filename is simply index.php because we placed the file directly in your www folder as opposed to in a subfolder.

Troubleshooting[edit]

  • Access denied: make sure you have set the permissions properly (see the previous section)
  • Not found: be sure to double check your URL
  • 500 or server error: be sure to verify that you have copied the entire contents of the index.php file; even a single missing semicolon or opening PHP tag can cause this error

If the issue persists, please follow the instructions in W1003_Help_Me!

Background[edit]

PHP (w3 Schools)

Introduction[edit]

Topic Headers[edit]

Key Concepts[edit]

Exercises[edit]

ExercisesExercisesIcon.png
  • Using, HTML and php, demonstrate your ability to:
    • Create a Hello, World! program which displays a php-generated message on a page served from your www directory.
    • Create a page with the numbers 1 ... 1000 each displayed on a new line

References[edit]