Having Script Problems - !isset

Having Script Problems - !isset

by W Page -
Number of replies: 12
Hello to All!

Can someone explain to me why I keep getting the following error,
"Parse error: parse error, unexpected T_STRING, expecting ',' or ';' on line 10" with the following script,

<?php

// This is how it should look if I use isset with the !

$mood_one = "happy";
$mood_two = "sad";

if (!isset($mood_one))
{
echo I am ".$mood_two;  //This will print out - I am sad - because !isset is happy
}

else

{
echo I am ".$mood_one; //This will print out - I am happy - because !isset is not happy
}

?>

WP1
Average of ratings: -
In reply to W Page

Re: Having Script Problems - !isset

by Tony Hursh -
Looks like you're missing some opening quotes on the two echo lines. They should be:

echo "I am ".$mood_two;  //This will print out - I am sad - because !isset is happy

and:

echo "I am ".$mood_one; //This will print out - I am happy - because !isset is not happy


respectively.

BTW, PHP is smart enough to substitute variables inside of strings, so you could even write these like this:

echo "I am $mood_one";

In reply to Tony Hursh

Re: Having Script Problems - !isset

by W Page -
Hi Tony!

Thanks, that took care of that.

If you can be a little more patient smile, I just need to ask a few more questions.

What about if the variable is empty? Would the following be correct?

<?php

// This is how it should look if I use isset with the ! and the variable is empty

$mood_one = "";

if (!isset($mood_one))

{
echo I am happy // If $variable is empty, [b]isset($variable) will equate to TRUE (Because it has been defined) and [b] stands for !. This will print out - I am happy
}

?>

  • Why do programmers like to use "isset"??
  • Is there a better way to do what it does?

NOTE: The "X" is being placed into the script by the editor in $mood_one. It should not be there.

WP1

In reply to W Page

Re: Having Script Problems - !isset

by Shane Elliott -
Picture of Core developers Picture of Plugin developers
You can always use the empty() function to test if a variable is set AND not empty. eg

<?php

if (empty($mood1))
{
// equates to true because $mood1 is not set
}

$mood1 = "";
if (empty($mood1))
{
// equates to true because $mood1 is empty
}

$mood1 = "something";
if (empty($mood1))
{
// equates to false because $mood1 is not empty
}

?>

Depending on your settings, PHP will display a warning message if you try to access a variable that isn't set. Therefore programmers use isset/empty to test that a variable is set before trying to access the data in it. That said PHP is quite smart. The following will work equally well:

if ($mood1)
{
// effectively the same as "if (!empty($mood1))" for most data types
// however will raise a warning message is $mood1 is not set
}


More info on empty function at:
http://php.net/manual/en/function.empty.php
In reply to Shane Elliott

Re: Having Script Problems - !isset

by W Page -

Thanks Shane!

I am getting this slowly but surely. smile

WP1

In reply to W Page

Re: Having Script Problems - !isset

by Danilo Curci -
Is it a quiz ?!?!

I'm guessing.......

line 10 is

echo I am ".$mood_two; //This will print out - I am sad -

and is like this one:

echo I am ".$mood_one; //This will print out - I am happy -

now: it should be


echo "I am ".$mood_two.""; //This will print out - I am sad -

and is like this one:

echo "I am ".$mood_one."; //This will print out - I am happy -

isn't it?

danilo

I did'nt look above wink





In reply to Danilo Curci

Re: Having Script Problems - !isset

by W Page -

Hi Curci!

It is not a quiz, just me trying to learn something.  Do take a look above.

WP1

In reply to Danilo Curci

Re: Having Script Problems - !isset

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi Danilo,

two negative points for you!! big grin

Your answer:
echo "I am ".$mood_one."; //This will print out - I am happy -
is wrong!!

You missed (unclosed) a quote (the second unnecessary one in your code), so:
  • -1 due to your missed (unclosed) quote.
  • -1 due to your unnecessary quotes.


Only a joke, of course...sorry! big grin wink smile
In reply to Eloy Lafuente (stronk7)

Re: Having Script Problems - !isset

by Janne Mikkonen -
and -1 for not using moodle coding style wink wink
In reply to Janne Mikkonen

Re: Having Script Problems - !isset

by Danilo Curci -
Mikkonen and Lafuente, ok ok, in the second line I made a wrong copy and paste!
but that was the idea:

echo "this is ".$variable."";

You say that it's unnecessary? but it's more elegant! This is also a style !!! What's about Moodle style ??
Another question is: W Page is now happy or sad ?

Anyway, now we have another definition for 'MOODle' big grin
In reply to Danilo Curci

Re: Having Script Problems - !isset

by W Page -
Hi Danilo!

WP1 is now happy. smile big grin

WP1 learned something.

Thanks to all.

WP1