SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    0 Post(s)
    0 Thread(s)

    Question Question for byo database driven php&mysql chapter 7

    what is the use of the <?php if(isset($jokes)) :?>

    in build your own database driven website php & mysql page 224 chapter 7 for jokes.html.php template

    i didn't see the difference for the result when i removed the statement from the template.

    here is the code in jokes.html.php

    PHP Code:
    <h1>Search Results</h1>
            <?php if (isset($jokes)): ?>
                    <tr><th>Joke Text</th><th>Options</th></tr>
                    <?php foreach ($jokes as $joke): ?>
                    <tr valign="top">
                        <td><?php htmlout($joke['text']); ?></td>
                            <form action="?" method="post">
                                    <input type="hidden" name="id" value="<?php
    ($joke['id']); ?>"/>
                                    <input type="submit" name="action" value="Edit"/>
                                    <input type="submit" name="action" value="Delete"/>
                    <?php endforeach; ?>
            <?php endif; ?>
            <p><a href="?">New search</a></p>
            <p><a href="..">Return to JMS home</a></p>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    0 Post(s)
    0 Thread(s)
    I'm not familiar with the book, but my guess is that there's a chance that the variable may not be defined at all under certain circumstances. You can verify this by looking to see if it's defined within some type of conditional block(all if statments and looping constructs are conditional by nature).

    If this was the case, foreach would complain with a warning because it expects an array, and it would be getting null instead. Some parts of the html table markup would also get output, which probably isn't visually desirable.

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    0 Post(s)
    0 Thread(s)
    isset() is normally used to determine if a variable is set and is not NULL.
    Generally it's a good practice of using isset() wherever whenever it feels necessary. It's importance comes into account when you are working on error_reporting(E_ALL) mode.

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts