Jump to content
Larry Ullman's Book Forums

Venkok

Members
  • Posts

    9
  • Joined

  • Last visited

Posts posted by Venkok

  1. Thank you, Larry. Now it's clear.

    I have another solution for Pursue 3, I feel like Christopher's solution is more straightforward and clean but I use the function which allows having less writing (without duplication "The total cost of driving"...). Can you please say a few wise words about my code? :)   
     

    // Check for form submission:
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    // Minimal form validation:   
        if (isset ($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency'], $_POST['average_speed'])
        && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) {
        
        // Calculate the results
        $cost = calculate_trip_cost($_POST['distance'], $_POST['efficiency'], $_POST['gallon_price']);
        function travelTime() {
            $hours = $_POST['distance']/$_POST['average_speed'];
            if ($hours > 24) {
                $days = $hours/24;
                $hours2 = $hours%24; # вывожу целочисленный остаток.
                echo number_format($days, 0) . ' days and ' . $hours2 . ' hours';
            } else {
            echo number_format($hours, 2) . ' hours'; }
            }
    
        // Print the results:
        echo '<div class="page-header">
        <h1>Total Estimated Cost</h1></div>
        <p>The total cost of driving ' . $_POST['distance'] . ' miles, averaging ' . $_POST['efficiency'] . 'miles per gallon, and paying
        an average of $' . $_POST['gallon_price'] . ' per_gallon, is $' . $cost . '. If you drive
    at an average of ' . $_POST['average_speed'] . ' miles per hour, the trip will take approximately ';
            echo travelTime();
            echo '</p>';
    } else {
        // invalid submitted values.
        echo '<div class="page-header">
        <h1>Error!</h1></div><p class="text-danger">Please enter a valid distance, price per gallon, and fuel efficiency.</p>';
    }
    } // End of main submission IF.

     

  2. On 3/12/2012 at 9:42 PM, margaux said:
    
    // Validate the gender:
    
    $gender = 'a'; //create a false value to test against
    $validGender = array('M'=>'Sir', 'F'=>'Madam'); // create an associative array of valid values
    if (isset($_REQUEST['gender']) && (array_key_exists($_REQUEST['gender'], $validGender))) { //the function array_key_exists lets you check if a value is set as a key in your array
    $gender = $_REQUEST['gender']; // store the valid gender
    $greeting = '<p><b>Good day, ' . $validGender[$gender] . ' !</b></p>'; // use the valid gender as the key to your array to access the appropriate title
    } else { // Unacceptable value.
     $gender = NULL;
     echo '<p class="error">Please select your gender (M/F)!</p>';
     }
    
     

     

    Hi! Why do we need this:

    $gender = 'a'; //create a false value to test against

    The script works well without it, please explain! :)

  3. Hello! 
    Please explain to me how gender validations from scripts 2.3 differ from the nested one in script 2.4.


    I have this in 2.3 (updated with NULL coalescing operator while practicing Pursue section):


    $gender = $_REQUEST['gender'] ?? NULL; 
       
      if ($gender == 'M') {
        $greeting = '<p><strong>Good day, Sir!</strong></p>';
      } elseif ($gender == 'F') {
        $greeting == '<p><strong>Good day, Madam!</strong></p>';
      } else {
        $gender = NULL;
        echo '<p class="error">Gender must be either "M" or "F"!</p>'; /* You may wonder how this last case 
        may be possible, considering the values are set in the HTML form. 
        If a malicious user creates their own form that gets submitted to your handle_form.php script 
        (which is very easy to do), they could give $_REQUEST[‘gender’] any value they want. */
      }

     

    and this in 2.4:


    if (isset($_REQUEST['gender'])) {
      $gender = $_REQUEST['gender'];
       
      if ($gender = 'M') {
        $greeting = '<p><strong>Good day, Sir!</strong></p>';
      } elseif ($gender = 'F') {
        $greeting = '<p><strong>Good day, Madam!</strong></p>';
      } else {
        $gender = NULL;
        echo '<p class="error">Gender must be either "M" or "F"!</p>'; /* You may wonder how this last case 
        may be possible, considering the values are set in the HTML form. 
        If a malicious user creates their own form that gets submitted to your handle_form.php script 
        (which is very easy to do), they could give $_REQUEST[‘gender’] any value they want. */
      }
    } else { // $_REQUEST['gender'] is not set.
      $gender = NULL;
      echo '<p class="error">You forgot to
      select your gender!</p>';
    }

    It seems that these scripts do the same job, or I just can't figure out the difference, please help me to understand it.
    And also I want to if we could use NULL coalescing operator in script 2.4 some way. Thank you! 

     

×
×
  • Create New...