Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

PHP getting the strings "true" or "false" and converting them to 1 or 0

I am an Android developer and I'm working mostly with Java but as I'm developing my own primitive API in PHP, I'm struggling a bit here.

When a user adds a new item to their collection I have these three boolean values in the MySQL database:

In my Java code, I'm sending the string values of these - either true or false

When inserting them to MySQL I have to convert them to 1 or 0 accordingly, but I don't seem to be able to do so

Here is my code:

$item_is_gift           = $_POST['item_is_gift'];
$item_condition_new     = $_POST['item_condition_new'];
$item_bought_from_place = $_POST['item_bought_from_place'];

if($item_is_gift = true){
    $item_is_gift = 1;
} else {
    $item_is_gift = 0;
    $item_price = 0;
}

if($item_condition_new = true) {
    $item_condition_new = 1;
} else {
    $item_condition_new = 0;
}

if($item_bought_from_place = true) {
    $item_bought_from_place = 1;
    $item_bought_from_user = 0;
}

When I'm looking at the database records, all of these always say 1.

Upvotes: 0

Views: 275

Answers (5)

Maiar Hawkin
Maiar Hawkin

Reputation: 1

You fail in the conditions of the if's. You are assigning instead of comparing...

if($item_is_gift == true){
...
if($item_condition_new == true) {
...
if($item_bought_from_place == true) {

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

You're receiving a string value, being either false or true. To check use the comparison operator: ==, not = (which is the assignment operator). And compare strings... you're comparing a boolean to a string:

if ($item_condition_new == 'true')

is what you're actually after. You should also check if the key in the $_POST array actually exists:

if (isset($_POST['item_condition_new']))
    $item_condition_new     = $_POST['item_condition_new'];

To avoid notices. Also process the input to be a tad more human-friendly: remove trailing/leading whitespace and accept both upper and lower-case chars:

$item_condition_new = trim($item_condition_new);//remove excess white-space
$item_condition_new = strtolower($item_condition_new);//make all lower-case

Then, you can do:

if ($item_condition_new == 'true')
{
    $item_condition_new = 1;
}
else
{
    $item_condition_new = 0;
}

A shorter way is using the ternary operator (but be careful with this!):

$item_condition_new = $item_condition_new == 'true' ? 1 : 0;

Upvotes: 2

Raffael Luthiger
Raffael Luthiger

Reputation: 2211

Within your "if" you are setting a variable:

if($item_is_gift = true){

But what you want is to compare them. You would do this either with:

if($item_is_gift == true){

or with:

if($item_is_gift === true){

The PHP manual has more info about the difference.

Furthermore you want to compare to a string. So you would have to write it like this:

if($item_is_gift == 'true'){

Upvotes: 4

hindmost
hindmost

Reputation: 7205

Try this:

$item_is_gift = isset($_POST['item_is_gift']) && $_POST['item_is_gift'] == 'true'? 1 : 0;
// and so on...

Upvotes: 0

askrich
askrich

Reputation: 598

Ah ha!

You're using "=" instead of "==".

Give that a go..

if($item_condition_new == true) {
    $item_condition_new = 1;
} else {
    $item_condition_new = 0;
}

Upvotes: 1

Related Questions