Using PHP Functions and Variables

As your use of PHP progresses in Expression Web 2, you will undoubtedly find yourself working in the Code pane more and more. Fortunately, Expression Web 2 has IntelliSense for PHP and some code completion tools that assist greatly.

In this exercise, you will use PHP functions and variables from both the Insert menu and in Code view by using IntelliSense.

Note

Using PHP Functions and Variables

USE the CH12 sample site you modified in the previous exercise.

OPEN the CH12 site if it isn’t already open.

  1. In the Folder List, expand the /inc folder, and double-click banner.php, which you created earlier in this chapter. Set your cursor in the Code pane before the first HTML tag (<h1>).

  2. On the Insert menu, point to PHP, and then click Code Block.

    Expression Web inserts a PHP code block into your page.

  3. Enter the following lines inside the code block to set up some variables of your own, and then save the page:

    $Copy="Copyright &copy; 2007&#8211;";
    $CurYear= date('Y'),
    $Holder="<a href='http://chrisleeds.com/contact'>Chris Leeds</a>";
    Using PHP Functions and Variables

    Now that you’ve declared those variables, you can use them anywhere.

  4. In the Folder List, double-click footer.php in the /inc folder to open it for editing.

    Next you will use a PHP echo statement to write the copyright line, based on the values set in the previous step.

  5. Set your cursor in the Design view of the page, just before the first line of text.

    Then on the Insert menu, point to PHP, and click Echo. A PHP echo statement is added to the page.

  6. Inside the echo statement, type the following code:

    "$Copy $CurYear $Holder";

    That line will take care of writing and updating the HTML in the page, based on what the actual year is.

  7. Highlight the original copyright statement—everything between the closing PHP tag (?>) and the line break tag (<br />)—and press . Then save your page.

    Using PHP Functions and Variables
  8. In the Folder List, click MyPHP.php, and then, on the toolbar, click the Preview button.

    Using PHP Functions and Variables

    Preview

    The browser now shows a copyright statement. It will update the current year automatically and if you needed to change the copyright holder or link, you could edit one location and have it automatically updated wherever the variable is used.

    Using PHP Functions and Variables
  9. Close your browser window and return to Expression Web 2. In the Folders List, double-click MyPHP.php to open it for editing.

  10. Set your cursor inside the <p> tag in the <div#content> section of the page. On the Insert menu, point to PHP, and then click If.

    A PHP code if statement is inserted into the page.

  11. With your cursor inside of the if statement, in Code view, enter an (, and then press to reveal the IntelliSense choices. In this case, press , and then press the key to select file_exists.

    Using PHP Functions and Variables
  12. Press to insert that variable. To see a tooltip for the variable, press .

    Using PHP Functions and Variables
  13. As indicated in the tooltip, finish out the line of code by putting the file name in the syntax shown:

    ('inc/Untitled_1.php'))

    Tip

    You must always press Ctrl+L to display the IntelliSense window, and display the tooltips by pressing Ctrl+Shift+Spacebar.

    At this point, you have successfully asked the server if a specific file exists by using PHP. In the following steps, you will add code to direct the server to write something into the page if that file exists, and something else if it doesn’t.

  14. Insert the next PHP instruction on the same line but begin and end it with a curly brace:

    {echo "the file exists";}
  15. Press to break to a new line, and then type the following:

    else {echo "The file does not exist";}
    Tip
  16. Save your page and preview it in a browser.

    Notice that the server has written The file does not exist into the page.

  17. Close your browser and return to Expression Web 2. Click the New Document arrow, and then click PHP.

    Tip

    New Document

  18. Save the page. In the Save As dialog box, save it to the /inc folder by using its default name, Untitled_1.php.

  19. Return to MyPHP.php and preview it in a browser.

    Because the file now exists, your statement executes differently.

    Tip
  20. On the Tools menu, click Page Editor Options. Then in the Page Editor Options dialog box that opens, click the IntelliSense tab.

    On this tab, you can enable or disable PHP function categories and customize the list to your preference. There are also features for PHP Global Variable Completion and PHP Parameter Information that you can enable or disable based on your preference.

Tip

Tip

You can customize the color scheme for PHP files on the Color Coding tab. There is a group of eight PHP items under the Display Items label that can be custom colored.

Note

Tip

CLOSE the CH12 Sample Site, and exit Expression Web 2.

Although the preceding exercise didn’t have much specific meaning programmatically, you should now have a higher comfort level working with PHP in Expression Web 2, whether in Code view or Design view, and you should be familiar with how IntelliSense for PHP can help you write PHP code more quickly and with less potential for errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.236.119