By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

finding compile time errors

P: n/a
As I'm learning PHP, I'm making a fair number of mistakes in syntax.

In perl, you can turn on reading these errors from the browser by
adding this:

use CGI::Carp 'fatalsToBrowser';

Is there something like this in PHP?

Or do I need to find the php.ini file and look to see where the error
log is?

I spent some time trying to figure out why my class constructor
crashed until I found that I was writing php5 on a php4 setup. What is
most of the established base, is it 4 or 5? I see in the php docs that
some examples use 5.3, considering that the latest stable release is 5.2
I can see how there might be a lot of hair lost!

Any recommendations on creating form elements? I've always used
CGI.pm which handles all the gotchas. Is there anything that comes with
the standard php install? All I really need is radio, select, checkbox
and text... I'm used to writing what I need, but I'm also used to not
reinventing a wheel that was written by someone far smarter!
Is there a standard include_path being used? Are there any standard
extensions for included libararies, generally I'd like them to be seen
*only* by the parent script.

Jeff
Jun 27 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
..oO(Jeff)
As I'm learning PHP, I'm making a fair number of mistakes in syntax.

In perl, you can turn on reading these errors from the browser by
adding this:

use CGI::Carp 'fatalsToBrowser';

Is there something like this in PHP?
In PHP error messages are printed to the browser by default (or to the
console if you use the CLI version). With the error_reporting directive
you can control which types of errors should be shown. On a development
machine this directive should be set to E_ALL|E_STRICT in the php.ini.
Also make sure that display_errors is turned on.
Or do I need to find the php.ini file and look to see where the error
log is?
By default there is no error log, but you can enable it.
I spent some time trying to figure out why my class constructor
crashed until I found that I was writing php5 on a php4 setup. What is
most of the established base, is it 4 or 5?
PHP 4 is dead, the last support for it will finally end in a few weeks.
After that there will be no more security fixes released. So there's
absolutely no point in writing PHP 4 scripts anymore. Use a recent 5.2
instead.
>I see in the php docs that
some examples use 5.3, considering that the latest stable release is 5.2
I can see how there might be a lot of hair lost!
5.3 will introduce two major features that were "backported" from PHP 6,
but it's not released yet. It's not unusual that the manual already
contains short notes or even full documentation about coming features,
because many people already use 5.3 or even 6 for testing.
Any recommendations on creating form elements? I've always used
CGI.pm which handles all the gotchas. Is there anything that comes with
the standard php install? All I really need is radio, select, checkbox
and text... I'm used to writing what I need, but I'm also used to not
reinventing a wheel that was written by someone far smarter!
There are various classes and libraries out there for creating and
maintaining forms with PHP. I can't recommend one, since I use my own
code. You could try a Google search.
Is there a standard include_path being used? Are there any standard
extensions for included libararies, generally I'd like them to be seen
*only* by the parent script.
Not sure what you mean.

Micha
Jun 27 '08 #2

P: n/a
On Fri, 13 Jun 2008 18:00:20 +0200, Jeff <jeff@spam_me_not.comwrote:
As I'm learning PHP, I'm making a fair number of mistakes in syntax.

In perl, you can turn on reading these errors from the browser by
adding this:

use CGI::Carp 'fatalsToBrowser';

Is there something like this in PHP?

Set these in the configuration:
display_errors = on
error_reporting = E_ALL | E_STRICT

You can also set these at runtime using ini_set(), but syntax errors will
be encountered _before_ the code is run, so it's no use trying to capture
those by setting it in the script itself. Use php.ini / httpd.conf /
..htaccess files to set that configuration.

You can also do a simple check from the command line if you have that
available:

php -l /the/file/to/check

(which is one of the first tools I set/create whenever using a new
(version of) an editor).
I spent some time trying to figure out why my class constructor
crashed until I found that I was writing php5 on a php4 setup. What is
most of the established base, is it 4 or 5?
PHP 4 is end of life, not even security updates will be added before
long...
I see in the php docs that some examples use 5.3, considering that the
latest stable release is 5.2 I can see how there might be a lot of hair
lost!
Yup, something to consider, there's no 'seperate' documentation, so
keeping in mind the 'since' notes are important :)
Any recommendations on creating form elements? I've always used
CGI.pm which handles all the gotchas. Is there anything that comes with
the standard php install? All I really need is radio, select, checkbox
and text... I'm used to writing what I need, but I'm also used to not
reinventing a wheel that was written by someone far smarter!
Couldn't help you there.
Is there a standard include_path being used?
Yes, it's deceptively named 'include_path', and can be set anywhere in
your code or configuration, keep in mind what the current working
directory is though.
Are there any standard extensions for included libararies, generally I'd
like them to be seen *only* by the parent script.
As soon as you include libraries/functions/classes in you PHP script,
their definitions will be usable from anywhere in the code (not instances
of objects of course, those obey scope). Namespaces will arrive in PHP6 I
believe.
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #3

P: n/a
Michael Fesser wrote:
.oO(Jeff)
Thanks again for your kind assistance. I feel like I'm getting somewhere.
>
> As I'm learning PHP, I'm making a fair number of mistakes in syntax.

In perl, you can turn on reading these errors from the browser by
adding this:

use CGI::Carp 'fatalsToBrowser';

Is there something like this in PHP?

In PHP error messages are printed to the browser by default (or to the
console if you use the CLI version). With the error_reporting directive
you can control which types of errors should be shown. On a development
machine this directive should be set to E_ALL|E_STRICT in the php.ini.
Also make sure that display_errors is turned on.

OK, I'll find a check the PHP.ini file as I'm finding that the scripts
silently fail.
>
> Or do I need to find the php.ini file and look to see where the error
log is?

By default there is no error log, but you can enable it.
> I spent some time trying to figure out why my class constructor
crashed until I found that I was writing php5 on a php4 setup. What is
most of the established base, is it 4 or 5?

PHP 4 is dead, the last support for it will finally end in a few weeks.
After that there will be no more security fixes released. So there's
absolutely no point in writing PHP 4 scripts anymore. Use a recent 5.2
instead.
OK. The box I'm working on was initialized in this year. I expected 5
something. This must just be a preference with my web host (RackSpace).
>
>I see in the php docs that
some examples use 5.3, considering that the latest stable release is 5.2
I can see how there might be a lot of hair lost!

5.3 will introduce two major features that were "backported" from PHP 6,
but it's not released yet. It's not unusual that the manual already
contains short notes or even full documentation about coming features,
because many people already use 5.3 or even 6 for testing.
Thanks. I like the direction PHP seems to be going, but there are so
many "functions"! I'm a bit overwhelmed!
>
> Any recommendations on creating form elements? I've always used
CGI.pm which handles all the gotchas. Is there anything that comes with
the standard php install? All I really need is radio, select, checkbox
and text... I'm used to writing what I need, but I'm also used to not
reinventing a wheel that was written by someone far smarter!

There are various classes and libraries out there for creating and
maintaining forms with PHP. I can't recommend one, since I use my own
code. You could try a Google search.
I'm striking out, but am thinking to roll my own also.
>
> Is there a standard include_path being used? Are there any standard
extensions for included libararies, generally I'd like them to be seen
*only* by the parent script.

Not sure what you mean.
If I name the included file 'my_functions.php' and put it in the web
path, wouldn't some of this be visible from the web. I just want nothing
to happen if someone hits http://my_domain.com/my_includes.php. Perhaps
I'm worrying about nothing...
How does PHP handle placeholders in MySQL?

I'm used to doing this:

$sql='SELECT some_field FROM some_table WHERE another_field = ? AND
other_field = ?';

When you do this (at least in perl), you don't have to worry about
SQL insertion.

I can't seem to find this, and had stumbled across an example
earlier. Perhaps I should break this question out in a new post...

Jeff
>
Micha
Jun 27 '08 #4

P: n/a
..oO(Jeff)
>If I name the included file 'my_functions.php' and put it in the web
path, wouldn't some of this be visible from the web. I just want nothing
to happen if someone hits http://my_domain.com/my_includes.php. Perhaps
I'm worrying about nothing...
The best and most secure way is to store such files outside the document
root, so that they can't be accessed by a URL.
>How does PHP handle placeholders in MySQL?

I'm used to doing this:

$sql='SELECT some_field FROM some_table WHERE another_field = ? AND
other_field = ?';

When you do this (at least in perl), you don't have to worry about
SQL insertion.

I can't seem to find this, and had stumbled across an example
earlier. Perhaps I should break this question out in a new post...
Have a look at PDO. It's the recommended and most flexible database
interface (installed and enabled by default). The keyword to look for
is "prepared statements".

Micha
Jun 27 '08 #5

P: n/a
On Fri, 13 Jun 2008 19:09:39 +0200, Jeff <jeff@spam_me_not.comwrote:
How does PHP handle placeholders in MySQL?

I'm used to doing this:

$sql='SELECT some_field FROM some_table WHERE another_field = ? AND
other_field = ?';

When you do this (at least in perl), you don't have to worry about
SQL insertion.
Yup, if you use either the mysqli extension or PDO.

http://nl2.php.net/manual/en/book.mysqli.php
http://nl2.php.net/manual/en/book.pdo.php

I'm not really a fan of how they altered the manual lately, but this
should still be easily found:
Manual =Function Reference =Database Extensions
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #6

P: n/a
Michael Fesser wrote:
.oO(Jeff)
>If I name the included file 'my_functions.php' and put it in the web
path, wouldn't some of this be visible from the web. I just want nothing
to happen if someone hits http://my_domain.com/my_includes.php. Perhaps
I'm worrying about nothing...

The best and most secure way is to store such files outside the document
root, so that they can't be accessed by a URL.
What I'd like is to be able to specify a library path and when I
require a file the script would know where to look if I just did:
require 'some_file.php';

Lets say I had a directory at the same level as my httpdocs directory
named library. How do I specify that?

I know this bit of pseudo code won't work...

set_include_path('../' . $_SERVER['DOCUMENT_ROOT'] . '/library');

I'm still having trouble with searching the manual, now I'm getting
half the results in Cyryllic!

>
>How does PHP handle placeholders in MySQL?
<snip>
>
Have a look at PDO.


Perfect. It looks a *lot* like the Perl DBI module which might be the
best thing that ever happened to perl!

I take it I can do this:

$dbh = new PDO($dsn, $user, $password);
$sql='SELECT * FROM some_table WHERE field_1 = ? AND field_2 =?';
$sth=$dbh->prepare($sql);
$sth->execute('field_1_value','field_2_value');

while($result = $sth->fetch(PDO::FETCH_ASSOC)){
echo $result['some_field'];
}

Is there a die in PHP with an error message? Or should I be using try
catch?

Jeff

It's the recommended and most flexible database
interface (installed and enabled by default). The keyword to look for
is "prepared statements".

Micha
Jun 27 '08 #7

P: n/a
..oO(Jeff)
>Michael Fesser wrote:
>.oO(Jeff)
>>If I name the included file 'my_functions.php' and put it in the web
path, wouldn't some of this be visible from the web. I just want nothing
to happen if someone hits http://my_domain.com/my_includes.php. Perhaps
I'm worrying about nothing...

The best and most secure way is to store such files outside the document
root, so that they can't be accessed by a URL.

What I'd like is to be able to specify a library path and when I
require a file the script would know where to look if I just did:
require 'some_file.php';

Lets say I had a directory at the same level as my httpdocs directory
named library. How do I specify that?

I know this bit of pseudo code won't work...

set_include_path('../' . $_SERVER['DOCUMENT_ROOT'] . '/library');

I'm still having trouble with searching the manual, now I'm getting
half the results in Cyryllic!
$_SERVER['DOCUMENT_ROOT'] contains the absolute path to the website. In
my own scripts I use it as the starting point to define some constants,
which point to my library directory (for example one level above the
docroot) and some of its important subdirectories, e.g.

define('LIB_PATH', $_SERVER['DOCUMENT_ROOT'].'/../lib');
define('LIB_MODULE_PATH', LIB_PATH.'/modules');
define('LIB_TEMPLATE_PATH', LIB_PATH.'/templates');

Then for the includes all it needs is something like

require_once LIB_MODULE_PATH.'/foo/bar.inc';

Nice and easy, and because of the absolute paths very reliable.
>>How does PHP handle placeholders in MySQL?

<snip>
>>
Have a look at PDO.

Perfect. It looks a *lot* like the Perl DBI module which might be the
best thing that ever happened to perl!

I take it I can do this:

$dbh = new PDO($dsn, $user, $password);
$sql='SELECT * FROM some_table WHERE field_1 = ? AND field_2 =?';
$sth=$dbh->prepare($sql);
$sth->execute('field_1_value','field_2_value');

while($result = $sth->fetch(PDO::FETCH_ASSOC)){
echo $result['some_field'];
}

Is there a die in PHP with an error message? Or should I be using try
catch?
Of course there's a way to kill a script with die() and an error
message, but on the long run a proper error/exception handling is
definitely the better way. die() can be useful for quick tests or for
debugging, but should not be used in production code. Other solutions
are much more flexible.

My own exception handler for example writes a debug backtrace to a
logfile (or to a named pipe on my Linux server), which makes it very
easy to find the file and location where the error occured.

Micha
Jun 27 '08 #8

P: n/a
Jeff wrote:
As I'm learning PHP, I'm making a fair number of mistakes in syntax.
-- SNIP --
>
Any recommendations on creating form elements? I've always used CGI.pm
which handles all the gotchas. Is there anything that comes with the
standard php install? All I really need is radio, select, checkbox and
text... I'm used to writing what I need, but I'm also used to not
reinventing a wheel that was written by someone far smarter!
Well, why not simply uses HTML, unlike PERL, PHP is similar to ASP,
where you can do:
<select name="selectList">
<?php foreach($records as $key=>$value) { ?>
<option value="<?php print $key; ?>"><?php print $value; ?></option>
<?php } ?>
</select>

However, this is a simplistic approach, you might also want to research
templating function
such as Smarty (smarty.php.net)
>
Is there a standard include_path being used? Are there any standard
extensions for included libararies, generally I'd like them to be seen
*only* by the parent script.
PHP does not do this yet (probably when namespaces implemented 6 or 5.3),
you may have the option to do that, but I'm still unclear on how they
are going to implement it.

In PHP there are PHP "extensions" (think of CPAN extension -- whereby
the extensions are compiled -- not PHP script).
(These is what PDO are BTW)... and they are available on ALL of the
included script, and you do not need to specifically
"tell" your script to "import" or "use" them.

While script "require" or "include" will tell PHP to also include script
file that you also want to execute.
Once included, depending on the contents and where you include it, the
contents of the script will be available
for the rest of the script (available after the inclusion point)....

See "require" or "include" on PHP manual to get more info on that.
Jeff
Hendri Kurniawan
Jun 27 '08 #9

P: n/a
Hendri Kurniawan wrote:
Jeff wrote:
> As I'm learning PHP, I'm making a fair number of mistakes in syntax.
-- SNIP --
>>
Any recommendations on creating form elements? I've always used
CGI.pm which handles all the gotchas. Is there anything that comes
with the standard php install? All I really need is radio, select,
checkbox and text... I'm used to writing what I need, but I'm also
used to not reinventing a wheel that was written by someone far smarter!

Well, why not simply uses HTML, unlike PERL, PHP is similar to ASP,
where you can do:
<select name="selectList">
<?php foreach($records as $key=>$value) { ?>
<option value="<?php print $key; ?>"><?php print $value;
?></option>
<?php } ?>
</select>
Sure but if most forms have many selects.

I'd like to just pass in an associative array to a function and have
it return the form widget.

My first php class is looking like this (feel free to add constructive
criticism):

class FormWidgets{

public function __construct($A){
if($A['type'] == 'select'){
return makePopupMenu($A);
}
}

public function makePopupMenu($A){

$values = $A['values'];
$labels = $A['labels'];

$content = '<select name="' . $A['name'] . '">' . "\n";

foreach($values as $item){
$selected_ = '';

if($A['value'] == $item){$selected_ = 'selected';}
$label_ = $labels[$item];
if(! $label_){$label_ = $item;}
$content .= '<option value="' . $item . '"' . $selected_ . '>' . label_
.. '</option>' . "\n";
}

$content .= '</select>';
return $content;

}
} // end class

You'd call it like this, I think:

$form_widget = new FormWidgets;

$popup_menu =
$form_widget->makePopupMenu('name'=>'some_name','values'=>array ('item
1','item 2'),'labels'=>array(...)...);

I can see a lot left out of this, like optional event handlers and
styles.

I would just include that in a heredoc that contains the form.
Perhaps that's too perlish...
>
Howev=>,er, this is a simplistic approach, you might also want to research
templating function
such as Smarty (smarty.php.net)

Thanks, I'll look at that.
>
>>
Is there a standard include_path being used? Are there any standard
extensions for included libararies, generally I'd like them to be seen
*only* by the parent script.

PHP does not do this yet (probably when namespaces implemented 6 or 5.3),
you may have the option to do that, but I'm still unclear on how they
are going to implement it.

In PHP there are PHP "extensions" (think of CPAN extension -- whereby
the extensions are compiled -- not PHP script).
(These is what PDO are BTW)... and they are available on ALL of the
included script, and you do not need to specifically
"tell" your script to "import" or "use" them.

Hmmm. I normally have a small configure file for each server which has
a database handle and paths in it. Can I just name a file with the pdo
extension and will php use it (if it's in the library path) without have
to call include or require? Should I be using pdo for my "module"
extension? Those "modules" will just have classes.
>
While script "require" or "include" will tell PHP to also include script
file that you also want to execute.
Once included, depending on the contents and where you include it, the
contents of the script will be available
for the rest of the script (available after the inclusion point)....

See "require" or "include" on PHP manual to get more info on that.
Do you know if the the include_once is like the perl 'do'?

Jeff
>
> Jeff

Hendri Kurniawan
Jun 27 '08 #10

P: n/a
Jeff wrote:
>
-- SNIP --
>
I'd like to just pass in an associative array to a function and have
it return the form widget.

My first php class is looking like this (feel free to add constructive
criticism):

class FormWidgets{

public function __construct($A){
if($A['type'] == 'select'){
return makePopupMenu($A);
}
}

public function makePopupMenu($A){

$values = $A['values'];
$labels = $A['labels'];

$content = '<select name="' . $A['name'] . '">' . "\n";

foreach($values as $item){
$selected_ = '';

if($A['value'] == $item){$selected_ = 'selected';}
$label_ = $labels[$item];
if(! $label_){$label_ = $item;}
$content .= '<option value="' . $item . '"' . $selected_ . '>' .
label_ .. '</option>' . "\n";
}

$content .= '</select>';
return $content;

}
} // end class

You'd call it like this, I think:

$form_widget = new FormWidgets;

$popup_menu =
$form_widget->makePopupMenu('name'=>'some_name','values'=>array ('item
1','item 2'),'labels'=>array(...)...);
Well, I kinda see how you want to do that, but IMHO you are not taking
advantage of the language,
as you are trying to do it PERL way... However there is one error that I
can spot on your class declaration
-- Constructor will always return an instance of the class no matter
what you return
>
-- SNIP --
>

Hmmm. I normally have a small configure file for each server which has
a database handle and paths in it. Can I just name a file with the pdo
extension and will php use it (if it's in the library path) without have
to call include or require? Should I be using pdo for my "module"
extension? Those "modules" will just have classes.
No, you need to use "include" or "require"...
Yes, you can use PDO to access database, as it provides abstraction
layer to access database.
There are some people who does not like to use it, citing performance
issues.
>
-- SNIP --
>
Do you know if the the include_once is like the perl 'do'?
Hmmm, PHP takes a more simplistic approach to it's include_once.
Anything that you tell PHP under include_once directive will simply
means to include the
specified files to be executed too... Please refer to PHP manual for
more info.

Hendri Kurniawan
Jun 27 '08 #11

P: n/a
Hendri Kurniawan wrote:
Jeff wrote:
>>
-- SNIP --
>>
I'd like to just pass in an associative array to a function and have
it return the form widget.

My first php class is looking like this (feel free to add constructive
criticism):

class FormWidgets{

public function __construct($A){
if($A['type'] == 'select'){
return makePopupMenu($A);
}
}

public function makePopupMenu($A){

$values = $A['values'];
$labels = $A['labels'];

$content = '<select name="' . $A['name'] . '">' . "\n";

foreach($values as $item){
$selected_ = '';

if($A['value'] == $item){$selected_ = 'selected';}
$label_ = $labels[$item];
if(! $label_){$label_ = $item;}
$content .= '<option value="' . $item . '"' . $selected_ . '>' .
label_ .. '</option>' . "\n";
}

$content .= '</select>';
return $content;

}
} // end class

You'd call it like this, I think:

$form_widget = new FormWidgets;

$popup_menu =
$form_widget->makePopupMenu('name'=>'some_name','values'=>array ('item
1','item 2'),'labels'=>array(...)...);

Well, I kinda see how you want to do that, but IMHO you are not taking
advantage of the language,
as you are trying to do it PERL way...
I only know the perl way (actually that was lifted from javascript). I'm
trying to learn the php way. Aside from that, I'm believer in templates
and that the core html should be separate from the programming. I'm a
couple days into learning php and I'd use the right style if I knew it!

However there is one error that I
can spot on your class declaration
-- Constructor will always return an instance of the class no matter
what you return
Thanks. I see now that my constructor is useless. It took me a long
time to figure out that I needed a closing semicolon, that a "}" did not
negate it's need.
>
>>
-- SNIP --
>>

Hmmm. I normally have a small configure file for each server which
has a database handle and paths in it. Can I just name a file with the
pdo extension and will php use it (if it's in the library path)
without have to call include or require? Should I be using pdo for my
"module" extension? Those "modules" will just have classes.

No, you need to use "include" or "require"...
OK. I guess require_once...
Yes, you can use PDO to access database, as it provides abstraction
layer to access database.
There are some people who does not like to use it, citing performance
issues.
Hmmm. I'm more concerned about my performance benefit! I understand PDO
and my guess is that once you create a database handle that you're over
the hump.
>
>>
-- SNIP --
>>
Do you know if the the include_once is like the perl 'do'?

Hmmm, PHP takes a more simplistic approach to it's include_once.
Anything that you tell PHP under include_once directive will simply
means to include the
specified files to be executed too... Please refer to PHP manual for
more info.
I'm trying. Sometimes it is damn hard to find it in the index.

Jeff
>
Hendri Kurniawan
Jun 27 '08 #12

P: n/a
Jeff wrote:
>
-- SNIP --
I only know the perl way (actually that was lifted from javascript). I'm
trying to learn the php way. Aside from that, I'm believer in templates
and that the core html should be separate from the programming. I'm a
couple days into learning php and I'd use the right style if I knew it!
Then you'll love Smarty (http://www.smarty.net/)...
I can't think of any other templating engine (others in the NG might now)...
I don't use it myself, because:
1/ Smarty is OLD -- It uses PHP4
2/ It has quite an overhead that I don't require
But I do base my templating engine based on Smarty

I'm pointing Smarty out to you, as it can provide a basis for your
templating,
and as the template is partly plain HTML, web designer ca easily read them
(with some learning of course)

What Smarty does:
$countries = array(
'au' ='Australia',
'us' ='United States',
'uk' ='United Kingdom'
);

$tpl = new Smarty();
$tpl->assign('SelectCountries', $countries);
$tpl->display('select_country_form.tpl');
>
-- SNIP --
Hmmm. I'm more concerned about my performance benefit! I understand PDO
and my guess is that once you create a database handle that you're over
the hump.
IMHO the performance is negligible compared to the benefit or
abstracting your database layer
and by the fact that PDO ships with PHP (5.1 onwards), means it will
quickly become the standard (IMHO)
>
-- SNIP --
Hendri Kurniawan
Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.