Title: Discussion Intro to PHP, ITSE 2402
Course: Intermediate Web Programming (25436)

IITSE 2402
Intro to PHP Chapter 1 Discussion
Discussion
1. PHP Server-Side Coding
  - PHP, like JavaScript, can be embedded directly into your HTML code, or you can create a separate PHP file and include that file in your HTML document.
  - If you have any PHP (even the include statement for PHP coding) you must use a php extension NOT htm or html.
  - You created a PHP page when you wrote your AJAX assignment. You will create more PHP files. When you create a separate file, do not start with your templateHTML5 page.
  - Once you have any PHP in your page, you cannot view that page directly on your browser. You can open your page in your XAMP server or upload to your site on the server to view.
  - Try this site for finding PHP syntax errors. If the checker doesn't work, to find PHP errors, it is recommended that you comment out all code and uncomment one line at a time for testing. Due to PHP being executed on the server before reaching your HTML code, it is very tedious to debug.
  - Do not put the validation images on any page that is being called by another page that has the validation images. That will result in double images on the page.
2. Standard PHP Script Delimiters
  - PHP code declaration blocks are written:
<?php
statements;
?>
  - There is no space between the ? and php. If you accidentally put a space, your code will not execute.
  - Each PHP statement ends with a ; If you omit the ; your code will not execute.
  - When you have a syntax error in your php code, your code will not execute. (And you thought finding JavaScript errors was tough?)
  - PHP does not require a single statement per line. However, it is highly recommended that you only write one statement per line so that anyone can more easily follow your code.
  - The example on step 4, page 5 of your PHP text shows a php block between p tags. It also shows a word wrap between script and delimiters.";
    1. You cannot word wrap text between quotes (just like JavaScript). This was done by the publisher and is not correct code.
    2. You can also write the html tags inside the echo statement. (Don't do both.) For example:
<?php
echo "<p>This text is printed using standard PHP script delimiters.</p>";
?>
3. Other PHP Code Blocks
  - You can use the script element for PHP code blocks. They are not used here. Just be aware that they exist.
  - Some servers support the short PHP script delimiter and/or the ASP-style delimiter. Our class server does not support either the short PHP or the ASP-style script delimiters.
4. Practice Using PHP
  - Create helloWorld1.php page
    - Open your templateHTML5.htm page in your editor.
    - Save your template as helloWorld1.php in your module4 folder.
    - Add a PHP code block that uses the echo statement to display "Hello World".
    - Save your helloWorld1.php document.
    - Either open in your XAMP server, or upload and look at your page. This practice is not graded.
5. External PHP files
  - External PHP files can be used any time you want to reuse the PHP code on multiple pages.
  - To access external PHP files, use the include statement. For example:

                           <?php
                           include 'welcome.php';
                           ?>

6. PHP Functions
  - There are both predefined and user defined functions. Both types of functions are written using the same rules that you used to write or use JavaScript functions.
  - PHP functions must be defined before they are called.
  - User defined PHP functions may be internal or external.
  - When an external file contains a function, you must first include the file, then you can call the function.
7. Displaying text in PHP
  - Use the echo() statement or the print() statement to create new text on a Web page. Both statements are built in language constructs. The print() statement actually returns a 1 if it is successful or a 0 if it is not successful.
  - To pass multiple arguments in either an echo() or print() statement, separate the arguments using a comma.
  - Using parenthesis to contain the arguments in an echo() or print() statement is optional.
8. Case Sensitivity
  - PHP code is usually not case sensitive. However, your code will be harder to debug if you or not consistent and it is probably less confusing to stick with lower case since XHMTL and JavaScript are case sensitive. It just helps train you to stick with lower case. Keep in mind that identifiers are case sensitive.
9. Comments
  - PHP uses both line comments and block comments exactly the same as JavaScript.
  - In addition to using comments to document you work, comments are a great diagnostic tool. Since incorrect PHP syntax and typos cause your page to not render, it sometimes seems impossible to find your errors. Sometimes you must comment out each line of code, then view the page. Then uncomment line by line (or when using selection a few lines together) and view after each change. Then you can pinpoint exactly which line is causing the problem. It is often such a tiny typo that it is very hard to find.
  - And, look for an error handling log file. Your errors should be listed there.
10. PHP Variables
  - Rules and Conventions for Naming Variables in PHP
    - Identifiers must begin with a dollar sign ($).
    - You can use numbers or an underscore (_) in an identifier, but not as the first character after the dollar sign.
    - You cannot include spaces in an identifier.
    - Identifiers are case sensitive.
  - Declaration and Initialization of Variables in PHP
    - In PHP you must declare and initialize a variable in the same statement.
    - Like JavaScript, PHP variables are loosely typed.
  - Displaying PHP Variables
    - To display a variable, the variable is passed to either echo() or print() without quotes. For example:

                         $VotingAge = 18;
                         echo $VotingAge;

    - To display both text and strings, you can separate the arguments using commas. For example:

                         echo "<p>The legal voting age is ", $VotingAge, ".</p>";

Displays
The legal voting age is 18.
    - You can also use variable names inside a text string, but the outcome differs whether you use single quotations or double quotations.

Using double quotations the code

                         echo "<p>The legal voting age is $VotingAge.</p>";

Also displays
The legal voting age is 18.

However, the code

                         echo '<p>The legal voting age is $VotingAge.</p>';

Displays
The legal voting age is $VotingAge.
11. PHP Constants
  - PHP constants do not begin with a $. By convention, use uppercase characters for the constant identifier.
12. External PHP files
  - External PHP files can be used any time you want to reuse the PHP code on multiple pages.
  - To access external PHP files, use the include statement. For example:

                      <?php
                      include 'welcome.php';
                      ?>

13. Practice creating an external PHP file that contains a function (do not start with your template). This is non-graded.
  - helpFunction.php
    - Create a php page with the following code (using your own name):

                  <?php
                  echo "<p>Welcome Your Name </p>";
                  function help()
                  {
                    echo "<p>How can I help you?</p>";
                  }
                  ?>

    - Save the document as helpFunction.php.
14. Including an external php file in your HTML (with the extension php) page.
  - welcome.php Page
    - Open your template.htm Page.
    - Save the page as welcome.php
    - Add the PHP code block below that includes your external php page welcome.php and then calls the function help().

                      <?php
                      include 'helpFunction.php';
                      help();
                      ?>

    - The include statement pulls the helpFunction.php external file into your HTML document at the location where you place your code. The statement help(); calls the function help() that was declared in your external page.
    - Save the document as welcome.php in your module4 folder.
    - View your pages at either localhost or your site.
    - Once you have made any necessary corrections, view your site, then view the source. Notice, your source is not php. It has been converted to html.
15. Using variables (these are arguments to the print and echo functions) for display
  - Open welcome.php
  - Save as welcomeVar.php
  - Add a variable $myName and assign the string value that is your name.
  - Change the display to write out the text Welcome and the variable $myName showing the value of $myName.
  - Save the document as welcomeVar.php in your module4 folder.