Solved

Strange PHP error

Posted on 2007-11-25
9
336 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

805 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