Solved

Strange PHP error

Posted on 2007-11-25
9
334 Views
Last Modified: 2009-12-16
               This code:

function possible($from, $to, $field, $color=$db['color']) {
echo($color);
//....
}

                gives this error:

<br />

<b>Parse error</b>:  syntax error, unexpected T_VARIABLE in <b>/var/www/chess/in.php</b> on line <b>113

</b><br />

                  Why?
0
Comment
Question by:Rok-Kralj
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 20345488
>function possible($from, $to, $field, $color=$db['color']) {
this is not possible , ie in the function declaration, to specify a default value from another variable.

you could do this:

function possible($from, $to, $field, $color='x') {
  if ($color === 'x') $color = $db['color'];

...
0
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 99 total points
ID: 20345489
The array $db does not exist within the scope of this function. Try something like this:

function possible($from, $to, $field, $color=null) {

if (!color)
{
   $color=$db['color'];
}

   echo($color);
//....
}
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20345490
$color=$db['color']  is not correct code.

What you want to do is

function possible($from, $to, $field, $color) {....

Then call the function with

possible($from, $to, $field, $db['color']);
0
 
LVL 24

Expert Comment

by:glcummins
ID: 20345491
Sorry, need to add

  global $db;

 to the top of the function.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Author Comment

by:Rok-Kralj
ID: 20345629
yodercm... Your solution is pointless. Because your script excludes functionality I wanted to acheve at first place.

Apart from his solution, thanks!
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20345666
Gee, I'm sorry you can't use it, even though it works just fine and answers your question as asked.  I don't mind not getting points if it didn't work for you, but your attitude and insulting comments, and deliberately excluding me from even a "thanks" for the attempt to help, make it extremely unlikely I'll ever try to help you again.  
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 20345675
Rok-Kralj,

Your first snippet of code actually did not give me  the idea that you have been working with php for a long time. The comment by yodercm therefor was one all experts could have made, by assuming your level of php expertise based on your own posting.

I agree with yodercm that your attitude is misplaced and does not motivate experts to help you in the future.

-r-
0
 
LVL 12

Author Comment

by:Rok-Kralj
ID: 20345737
Sorry, youdercm, but I was asking about tehnical reason why this is not working... Answer good enough would be:

Giving default value as variable to function is invalid.

I just told why your answer is not suitable... At least it is not the first.

This invention of ex-ex in to use:
Rok-Kralj, Qualified Expert
Guru on this subject.

And about saying thanks at the end... It is not common here. Only one asker did it in my whole career here.

Thanks to all of you!
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20345870
And since my statment :  

>> $color=$db['color']  is not correct code.

is equivalent to your statement :  

>> Giving default value as variable to function is invalid.

I have to disagree that my answer was "not suitable".  

And while it was not the "first", it was posted simultaneously with two others.

However, forget who got points, the thing that disturbs me even still is your attitude with the statement :

>> And about saying thanks at the end... It is not common here. Only one asker did it in my whole career here.

I think you better look around.  It's very rare not to get thanked, even when my answer is not chosen, and rarer still to be insulted for posting a potential answer.  

Experts on EE are volunteers, and aside from the occasional T-shirt, they get no reward except the thanks of those whom they try to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now