How to force my variable in php?

0
if ($stmtMoreInformations->execute()) {

    $stmtUpdateMoreInformations = $conn->prepare("UPDATE menu SET only_delivery = :delivery, card_on_delivery = :delivery,
                                                 wifi = :wifi, live_music = :music, open_holiday = :holiday, 
                                                 acessible = :acessible
                                                 WHERE menu_id = :menu");

    $stmtUpdateMoreInformations->bindValue(":menu", $menu);
}

I have this update and my variables are returning 1 or 2, I want to force them to return true or false , can I use cast ? How could I use it?

$menu = $_POST['menu'];
    $delivery = null;
    if (isset($_POST['delivery'])) {
        $delivery = $_POST['delivery'];
    }

    $cards = null;
    if (isset($_POST['cards'])) {
        $cards = $_POST['cards'];
    }
    $wifi = null;
    if (isset($_POST['wifi'])) {
        $wifi = $_POST['wifi'];
    }
    $music = null;
    if (isset($_POST['music'])) {
        $music = $_POST['music'];
    }
    $holiday = null;
    if (isset($_POST['holiday'])) {
        $holiday = $_POST['holiday'];
    }
    $acessible = null;
    if (isset($_POST['acessible'])) {
        $acessible = $_POST['acessible'];
    }







$stmtMoreInformations = $conn->prepare("SELECT only_delivery, card_on_delivery, wifi, live_music, 
                                        open_holiday, acessible FROM public.menu
                                        WHERE menu_id = :menu");

$stmtMoreInformations->bindValue(":menu", $menu);



if ($stmtMoreInformations->execute()) {

    $stmtUpdateMoreInformations = $conn->prepare("UPDATE menu SET only_delivery = :delivery, card_on_delivery = :card,
                                                 wifi = :wifi, live_music = :music, open_holiday = :holiday, 
                                                 acessible = :acessible
                                                 WHERE menu_id = :menu");

    $stmtUpdateMoreInformations->bindValue(':menu', $menu);
    $stmtUpdateMoreInformations->bindValue(':delivery', $delivery);
    $stmtUpdateMoreInformations->bindValue(':card', $cards);
    $stmtUpdateMoreInformations->bindValue(':wifi', $wifi);
    $stmtUpdateMoreInformations->bindValue(':music', $music);
    $stmtUpdateMoreInformations->bindValue(':holiday', $holiday);
    $stmtUpdateMoreInformations->bindValue(':acessible', $acessible);
    $stmtUpdateMoreInformations->execute();

    echo'delivery: '.$delivery;
    echo'card: '.$cards;
    echo'wifi: '.$wifi;
    echo'music: '.$music;
    echo'd: '.$holiday;
}
    
asked by anonymous 26.09.2016 / 13:54

1 answer

2

About converting to bool:

There are several ways, the ideal is to cast at the time of use and not return.

Some possibilities:

$boolMenu = ( $menu == 2 ); // só é true quando for 2

$boolMenu = (boolean) ( $menu - 1 );  // alterna entre zero (false) e 1 (true)

Applying to your POST, you can make it even simpler:

$cards     = isset($_POST['cards']    ) && ($_POST['cards']    =='2');
$wifi      = isset($_POST['wifi']     ) && ($_POST['wifi']     =='2');
$music     = isset($_POST['music']    ) && ($_POST['music']    =='2');
$holidays  = isset($_POST['holydays'] ) && ($_POST['holydays'] =='2');
$acessible = isset($_POST['acessible']) && ($_POST['acessible']=='2');

These lines above replace all of your question code. No more initialization, no cast, nothing.

With 'Yes' or 'No', it's the same logic:

$acessible = isset($_POST['acessible']) && ($_POST['acessible']=='Yes');

Always using the true value in == , being careful to leave the case the same as typed, since YES and Yes are completely different things to compare.

If you want to overdo it, you can still add && is_string( ... ) shortly after isset to sanitize arrays , but this does not normally happen in a normal form. Remember that the quotes in '2' are because GET and POST are always passed as string

    
26.09.2016 / 13:58