Solved

Strange PHP error

Posted on 2007-11-25
9
337 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 143

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:glcummins
ID: 20345491
Sorry, need to add

  global $db;

 to the top of the function.
0
 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 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