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
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";
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";
Hi Tony!
Thanks, that took care of that.
If you can be a little more patient , I just need to ask a few more questions.
NOTE: The "X" is being placed into the script by the editor in $mood_one. It should not be there.
WP1
Thanks, that took care of that.
If you can be a little more patient , 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
}
?>
<?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
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
<?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
Thanks Shane!
I am getting this slowly but surely.
WP1
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
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
Hi Curci!
It is not a quiz, just me trying to learn something. Do take a look above.
WP1
Hi Danilo,
two negative points for you!!
Your answer:
You missed (unclosed) a quote (the second unnecessary one in your code), so:
Only a joke, of course...sorry!
two negative points for you!!
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!
and -1 for not using moodle coding style
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'
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'
Coding guide ( to keep mr. Burns happy )
Hi Janne!
Thanks for the reminder.
WP1
Thanks for the reminder.
WP1
Hi Danilo!
WP1 is now happy.
WP1 learned something.
Thanks to all.
WP1
WP1 is now happy.
WP1 learned something.
Thanks to all.
WP1