# Construct Request

- Thread: https://externals.io/message/9187
- Original archive: https://news-web.php.net/php.internals/9187
- Started by Jason Garber on 2004-04-15T15:18:55+00:00
- Messages: 15
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #9187 — Jason Garber — 2004-04-15T15:18:55+00:00

URL: https://externals.io/message/9187#9187

Hello Internals team,

Thank you for taking a moment to evaluate a serious request by a serious
php developer that is responsible for a development company of 15 employees.

In order to keep our code as clean and error free as possible, we opt to
develop in the `E_ALL` error mode, and register_globals = off. This comes
with several challenges that one does not normally encounter.

We define and cast incoming script data variables at the top of each script,
so they are always defined and contain a valid value. Before `E_ALL` was
used, the form of this was:

$nCustID = (integer) $_POST['nCustID'];

and from that point on, the variables $nCustID and $sName would be used.
However when we started using E_ALL, these lines of code would throw
 `E_NOTICE` level errors when the variable did not exist in the $_POST array.
So now, we use the form:

$nCustID = (integer) (isset($_POST['nCustID']) ? $_POST['nCustID'] : 0));

I know that writing a line like the following would be a solution, but when
evaluated closely, it is a very slow and clumsy one, especially if you are
using a custom error handler.

@ $nCustID = (integer) $_POST['nCustID'];


There is nothing wrong with writing lines of code like
$nCustID = (integer) (isset($_POST['nCustID']) ? $_POST['nCustID'] : 0));
everywhere, but it does tend to make the code very messy.

The feature that I am proposing is a language construct, and therefore would
need integrated into the ZEND engine. It is very simple, and would be
modeled after isset().

Function:
setor(arg1, arg2) -- if parameter 1 is set, then return it, else evaluate
and return parameter 2.

Example:
$nCustID = (integer) setor($_POST['nCustID'], 0);


I would imagine that the ( ? : ) operator only evaluates the second or third
argument if they are actually used, and the setor() function should behave
the same. Therefore, it would allow for expressions such as:

echo setor($required_variable, die('error...');
or
echo setor($error, '');
or
echo setor($sMessage, $sDefaultMessage).
or
$z = setor($_GET['z'], 'Default');

As you can see, it would reduce and un-duplicate quite a bit of code, making
PHP an even more easy to use language. Please evaluate this request
carefully, and then let me know your thoughts on it.

Sincerely,

Jason Garber
President and Chief Technology Officer
IonZoft, Inc. :: 814.742.8030

---

## #9188 — Justin Hannus — 2004-04-15T15:28:29+00:00

URL: https://externals.io/message/9187#9188 · In reply to: #9187

> echo setor($required_variable, die('error...');
> or
> echo setor($error, '');
> or
> echo setor($sMessage, $sDefaultMessage).
> or
> $z = setor($_GET['z'], 'Default');
> $z = setor($_GET['z'], 'Default');

Whats wrong with defining a user-level function?

function setor_array(&$array, $key, $default = 0)
{
    return isset($array[$key']) ? $array[$key] : $default;
}

$nCustID = (int) setor_array($_GET, 'nCustID', 0);

---

## #9189 — Jason Garber — 2004-04-15T15:47:13+00:00

URL: https://externals.io/message/9187#9189 · In reply to: #9187

At 4/15/2004 11:28 AM -0400, Justin Hannus wrote:
> > […]
>
>Whats wrong with defining a user-level function?
>
>function setor_array(&$array, $key, $default = 0)
>{
>     return isset($array[$key']) ? $array[$key] : $default;
>}
>
>$nCustID = (int) setor_array($_GET, 'nCustID', 0);

My issue with defining a user level function is:

a. the actual variable in question could not be passed
b. it incurs the overhead of calling a user-level function

I do admit that this may be a plausible solution for the majority of my 
examples in initializing the incoming script variables, but does not 
address the general problem of developing with `E_NOTICE` turned on - for 
instance, accessing globals or deep arrays that may or may not exist.

It is one of the great features of PHP - accessing an undefined variable, 
but one that is completely removed when `E_NOTICE` is turned on.


What is the overhead of calling a simple UDF in php?

~Jason Garber

---

## #9191 — Derick Rethans — 2004-04-15T15:54:46+00:00

URL: https://externals.io/message/9187#9191 · In reply to: #9189

> a. the actual variable in question could not be passed
> b. it incurs the overhead of calling a user-level function

Which takes about as much time as calling an internal function, so that
argument is bogus.

Derick

---

## #9192 — Ilia Alshanetsky — 2004-04-15T16:01:30+00:00

URL: https://externals.io/message/9187#9192 · In reply to: #9187

I am not sure what would be the best name for such an engine construct or a 
function, but the idea itself is good (IMHO). It would certainly make it 
easier to write safer notice free code.

Ilia

---

## #9193 — Sascha Schumann — 2004-04-15T16:18:09+00:00

URL: https://externals.io/message/9187#9193 · In reply to: #9192

That is basically the same idea once proposed as extending
    the ternary operator (credit goes to GCC and its C extension).

    $bar ? : $baz;

    If $bar evaluates to true, the expression evaluates to $bar
    or to 0, respectively.

    - Sascha

---

## #9203 — Sean Coates — 2004-04-15T18:09:30+00:00

URL: https://externals.io/message/9187#9203 · In reply to: #9193

Is there a good reason to NOT implement this?
If not, I'm +1 on that (if I carry ANY weight at all (-: )

BC isn't an issue, and it would be a very useful feature, IMHO..

S

Sascha Schumann wrote:
>     That is basically the same idea once proposed as extending
>     the ternary operator (credit goes to GCC and its C extension).
> 
>     $bar ? : $baz;
> 
>     If $bar evaluates to true, the expression evaluates to $bar
>     or to 0, respectively.
> 
>     - Sascha

---

## #9212 — Hartmut Holzgraefe — 2004-04-15T19:30:33+00:00

URL: https://externals.io/message/9187#9212 · In reply to: #9188

Justin Hannus wrote:
> Whats wrong with defining a user-level function?

actualy in this special case i have been whishing for an operator for ages,
maybe a modification of the good old ternary:

   $foo = ?$bar : "default";

or

   $foo ?= "default";

;)

-- 
Hartmut Holzgraefe  <hartmut@php.net

---

## #9215 — Timm Friebe — 2004-04-15T19:35:18+00:00

URL: https://externals.io/message/9187#9215 · In reply to: #9214

> Quoting Hartmut Holzgraefe <hartmut@php.net>:
[...]
> If this were added, wouldn't it make sense to use the convention 
> already adopted
> by perl?
> 
> $foo |= 'default';

Already used:

$ php -r '$a= 1; $a |= 2; var_dump($a);'
int(3)

- Timm

---

## #9213 — Olivier Hill — 2004-04-15T19:36:37+00:00

URL: https://externals.io/message/9187#9213 · In reply to: #9212

Hartmut Holzgraefe wrote:
>  
> or
> 
>   $foo ?= "default";

Looks Too Much Like Perl. IMO, if this is to be implemented, a function 
should be created, and not a set of operators.

Olivier
-- 
GB/E/IT d+ s+:+ a-- C++$ UL++++$ P++++ L+++$ E- W++$ N- ?o ?K w--(---) 
!O M+$ V- PS+ PE- Y PGP t++ 5-- X+@ R- tv++ b++(+++) DI++++ D+ G++ e+>++ 
h(*) r y+(?)

---

## #9214 — Chuck Hagenbuch — 2004-04-15T19:36:47+00:00

URL: https://externals.io/message/9187#9214 · In reply to: #9212

Quoting Hartmut Holzgraefe <hartmut@php.net>:

> actualy in this special case i have been whishing for an operator for ages,
> maybe a modification of the good old ternary:
>
>    $foo = ?$bar : "default";
>
> or
>
>    $foo ?= "default";

If this were added, wouldn't it make sense to use the convention 
already adopted
by perl?

$foo |= 'default';

-chuck

--
"Regard my poor demoralized mule!" - Juan Valdez

---

## #9217 — Hartmut Holzgraefe — 2004-04-15T19:47:59+00:00

URL: https://externals.io/message/9187#9217 · In reply to: #9193

Sascha Schumann wrote:
>     That is basically the same idea once proposed as extending
>     the ternary operator (credit goes to GCC and its C extension).
> 
>     $bar ? : $baz;

yes, that looks like the way it should look like,
and the danger of newbees abusing it would be way lower
than with a function ... ;)


-- 
Hartmut Holzgraefe  <hartmut@php.net

---

## #9218 — George Schlossnagle — 2004-04-15T19:55:57+00:00

URL: https://externals.io/message/9187#9218 · In reply to: #9215

> 
> > […]
> [...]
> > […]
>
> Already used:
>
> $ php -r '$a= 1; $a |= 2; var_dump($a);'
> int(3)


perl's is ||= anyway.

George

---

## #9224 — Wez Furlong — 2004-04-15T20:21:55+00:00

URL: https://externals.io/message/9187#9224 · In reply to: #9217

+1 for the GCC style syntax.

--Wez.

----- Original Message ----- 
From: "Hartmut Holzgraefe" <hartmut@php.net>
To: "Sascha Schumann" <sascha@schumann.cx>
Cc: "Ilia Alshanetsky" <ilia@prohost.org>; <internals@lists.php.net>
Sent: Thursday, April 15, 2004 8:47 PM
Subject: Re: [PHP-DEV] Construct Request


> Sascha Schumann wrote:
> > […]
> 
> yes, that looks like the way it should look like,
> and the danger of newbees abusing it would be way lower
> than with a function ... ;)

---

## #9234 — Marcus Boerger — 2004-04-15T21:27:57+00:00

URL: https://externals.io/message/9187#9234 · In reply to: #9188

Hello Justin,

Thursday, April 15, 2004, 5:28:29 PM, you wrote:

> > […]

> Whats wrong with defining a user-level function?

> function setor_array(&$array, $key, $default = 0)
> {
>     return isset($array[$key']) ? $array[$key] : $default;
> }

> $nCustID = (int) setor_array($_GET, 'nCustID', 0);


Such a user function would be todays best practice since
neither a userfunction setor($val, $else) nor an internal implementation
of such a function would work. Here Derick's argument comes into place that
we cannot simply change the way variables and array offsets are accessed.

The above setor_array() doesn't have that problem though.

-- 
Best regards,
 Marcus                            mailto:helly@php.net
