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
In reply to W Page
Re: Having Script Problems - !isset
ó $a->name - $a->date
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";
In reply to Tony Hursh
Re: Having Script Problems - !isset
ó $a->name - $a->date
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
In reply to W Page
Re: Having Script Problems - !isset
ó $a->name - $a->date
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
In reply to Shane Elliott
Re: Having Script Problems - !isset
ó $a->name - $a->date
Thanks Shane!
I am getting this slowly but surely.
WP1
In reply to W Page
Re: Having Script Problems - !isset
ó $a->name - $a->date
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
In reply to Danilo Curci
Re: Having Script Problems - !isset
ó $a->name - $a->date
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
ó $a->name - $a->date
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!
In reply to Eloy Lafuente (stronk7)
Re: Having Script Problems - !isset
ó $a->name - $a->date
and -1 for not using moodle coding style
In reply to Janne Mikkonen
Re: Having Script Problems - !isset
ó $a->name - $a->date
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'
In reply to Danilo Curci
Re: Having Script Problems - !isset
ó $a->name - $a->date
Coding guide ( to keep mr. Burns happy )
In reply to Janne Mikkonen
Re: Having Script Problems - !isset
ó $a->name - $a->date
Hi Janne!
Thanks for the reminder.
WP1
Thanks for the reminder.
WP1
In reply to Danilo Curci
Re: Having Script Problems - !isset
ó $a->name - $a->date
Hi Danilo!
WP1 is now happy.
WP1 learned something.
Thanks to all.
WP1
WP1 is now happy.
WP1 learned something.
Thanks to all.
WP1