Jump to content

Recommended Posts

Can you find the error in this function?

 

 1 function insertClause($fields) {
2   $flist = $vlist = "";
3   foreach ( $fields AS $field=>$value ) {
4      if ($flist) {
5         $flist .= ",";
6      }
7      if ($vlist) {
8         $vlist .= ",";
9      }
10      $flist .= $field;
11      if ( is_null($value) ) {
12         %vlist .= "NULL";
13      }
14      elseif ( is_numeric($value) ) {
15         $vlist .= $value;
16      }
17      else {
18         $vlist .= "'{addslashes($value)}'";
19      }
20   }
21   return "$flist VALUES ($vlist)";
22 }

 

The PHP engine can't. It marks line 22 and displays seven different messages about what it expected but didn't get.

 

The first variable on line 15 begins with a '%' instead of a '$'.

 

PHP's error checking is often useless. The messages are so verbose and technical, and often so misplaced, that they convey nothing more specific than "something's wrong."

 

How do you figure out what the problem is in these difficult cases?

 

The only technique that I've found useful is to delete various parts of the code to see what happens. If the error message goes away, the must be in the part I deleted.

 

Have you found any other useful techniques?

Link to comment
https://forums.phpfreaks.com/topic/241951-finding-syntax-errors/
Share on other sites

Taken out of context, just the code you posted produces the following error -

 

Parse error: syntax error, unexpected '%' in your_file.php on line 13

 

That's pretty specific to what is wrong in that block of code (the problem on line 12 in what you posted is on line 13 in the error message because of a <?php tag on line one.)

 

If you are getting other syntax errors, it's likely your actual code that contains the posted code, also contains other syntax errors.

 

The language parser doesn't know what you (the programmer writing the code) intends. When you leave out some syntax or use the wrong syntax, the parser doesn't know what you were trying to do at that point. What you typed 'could' have been valid, depending on what else you did or didn't do in the following code. For the specific example you posted, maybe you were trying to start another if() test with a % mod operator in it, but left off or cut off the leading part of the line. How could the parser possibly know what you left out or typed incorrectly? So yes, a lot of reported syntax errors cannot be more specific than "something was wrong before this point in the code." It's up to you (the programmer) to proofread through your code, prior to where the error is reported, to make sure you typed what you intended.

 

My advice, don't write huge blocks of code all at once, then try to see if the whole thing works. Write code in smaller blocks, testing your code as you create each block or section. Use an incremental approach, rather than an all at once approach.

I think you're making my question much more complex than it is. I selected the code to illustrate the problem I'm asking about, and like any good illustration, it presents the problem in its pure form. Speculations about what would happen if there were errors in other parts of the code are just distracting. (To be clear, there were no other errors in the file. By missing that, you missed a large part of the point I was making.)

 

You give some good advice about writing and debugging a little code at a time, but that approach has limitations. In my experience it's not always a natural way to work; it can force me to write code an an order that's unnatural for the logic I'm trying to implement, and that costs more work, on the average, than it saves. I'm hoping to find additional tools.

I ran the same test, and I only get 1 error stating the exact line under where it is at. I am confused as to what you are getting at with the error message. Can you post the full context of the code (Without line numbers preferably) so we can run exactly what you are running and see what you are seeing?

 

Just giving us a function without the real context, does not help us figure out what you are talking about.

I ran the same test, and I only get 1 error stating the exact line under where it is at. I am confused as to what you are getting at with the error message. Can you post the full context of the code (Without line numbers preferably) so we can run exactly what you are running and see what you are seeing?

 

Is there a way to do that without writing it down by hand, then typing it in again? I've tried to copy the contents of the balloon, but I haven't found a way to do it. If I even switch windows to type in what I see directly, the balloon closes.

 

I'm using NetBeans 6.9.1, under Windows XP, with PHP 5.2.5.

You can use the php built in syntax checker from the command line.  Run the file that the function is in like php -l filename or just copy and paste the function in a separate file.

 

I tried that, with mixed results. I did get a one-line message that nailed the syntax error. (I wonder why the IDE displays a half-screen of bloviation instead?) But I also got a couple of dozen warnings in the nature of "Unable to initialize module." They came in sets which indicate, in each case, that I'm trying to load a thread-safe module with a non-thread-safe version of PHP.

 

I don't know what would happen to these messages if they occurred when Apache loads PHP, but it seems unlikely that they do. Some of them are for basic modules like openssl and mysqli. If those weren't being loaded I'd know it, because none of my applications would run.

 

Here's an example of what I get:

 

PHP Warning:  PHP Startup: gd: Unable to initialize module

Module compiled with module API=20060613, debug=0, thread-safety=1

PHP    compiled with module API=20060613, debug=0, thread-safety=0

These options need to match

in Unknown on line 0

PHP Warning:  PHP Startup: mysqli: Unable to initialize module

Module compiled with module API=20060613, debug=0, thread-safety=1

PHP    compiled with module API=20060613, debug=0, thread-safety=0

These options need to match

in Unknown on line 0

PHP Warning:  PHP Startup: openssl: Unable to initialize module

Module compiled with module API=20060613, debug=0, thread-safety=1

PHP    compiled with module API=20060613, debug=0, thread-safety=0

These options need to match

 

I tried loading PHP with the default directory pointing to the PHP directory, which contains php.ini as well as php.exe, instead of the source code directory. I got different errors, but the same general type.

 

This is a puzzlement. Before I make a project of researching where PHP searches for modules, what it does when it can't load them, and where the messages go, can anyone tell me from experience what is wrong?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.