Solved

Strange PHP error

Posted on 2007-11-25
9
331 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
Comment Utility
>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
Comment Utility
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
Comment Utility
$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
Comment Utility
Sorry, need to add

  global $db;

 to the top of the function.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Author Comment

by:Rok-Kralj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

772 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

14 Experts available now in Live!

Get 1:1 Help Now