|
Posted by Koncept on 10/11/00 11:57
In article <1157394155.661818.105050@p79g2000cwp.googlegroups.com>,
kenoli <kenoli@igc.org> wrote:
> Actually, some of it came up in some code you (Koncept-thanks) sent me
> recently. Here are some examples:
>
> preg_match('/^\d+$/',$v) && $q[] = "number = '{$v}'";
>
This uses a short-circuit operation, and like you have correctly
explained below, when the regExp evaluates to TRUE, the array on the
right hand side ($q) is either created or appended to. There are two
things to point out otherwise:
1) I am building an array of strings in the form of "number=(n)", I am
not performing associative array assignment, which would use the "=>"
operator. I literally mean to assign string values such as:
$q[0]= (string) "number=0";
$q[1] = (string) "number=1";
....
I believe that this circumstance this code was originally used to build
a SQL statement perhaps?
2) The braces around the $v are not necessary in the instance referred
to above. Sometimes I include code in this manner because my editor
highlights these regions for me more clearly. The only case where you
really have to use braces is in a situation where you have a variable
and you are using it in a string content (like making it's value plural
or something):
Consider the following (where $type = "book"):
<?php echo "I have 10 $types"; ?> // fails no variable $types
But this:
<?php echo "I have 10 {$type}s"; ?> // does what is expected.
These conditions are evaluated from left to right and are referred to
as "logical operations".
> TRUE || FALSE && $q[] = "number = '{$v}'"; //doesn't set the array
a) Does TRUE == TRUE? Yes it does. Stop the operation.
> FALSE || TRUE && $q[] = "number = '{$v}'"; //sets the array
a) Does FALSE == TRUE? Nope. Take a look at what is on the right
Another example (left associatively)
$a = true ? 0 : true ? 1 : 2; // ( true ? 0 : true ) ? 1 : 2 = 2
> What is the syntax here? I would think they should both set the array.
> Can you link any number of functions that resolve to true or false with
> operators like this? Which operators? What rules govern the construct?
> What precedence??
Here is a list of operators listing the precedence of operators with
the highest-precedence at the top. Operators on the same line have
equal precedence.
new
[
++ --
! ~ - (int) (float) (string) (array) (object) @
* / %
+ - .
<< >>
< <= > >=
== != === !==
&
^
|
&&
||
? :
= += -= *= /= .= %= &= |= ^= <<= >>=
and
xor
or
,
> Also, you used the format:
>
> $q[] = "number = '{$v}'";
>
> I think the more conventional is:
>
> $q[] = "number => '{$v}'";
>
> Also, I think:
>
> $q = array("number => '{$v}'");
See my note above about this. I was actually building a array values as
literal strings, not creating an associative array. In that case, I
probably would have done this:
$q = array('number' => 1);
or
$q['number'] = 1;
> Is more conventional for initializing an array. I have seen the
> syntax you used for concatenating elements to an existing array (which
> I guess actually takes place later in the code you sent as this is part
> of a "switch."). I suppose your usage is the same as adding an element
> to an array with no elements but this one hasn't been declared yet. I
> know php is loose about declaring variables. At any rate, it works.
You do not have to declare array values. PHP automatically creates them
when necessary. From the array() function docs:
"Syntax "index => values", separated by commas, define index and
values. index may be of type string or numeric. When index is omitted,
an integer index is automatically generated, *starting* *at* *0*. If
index is an integer, next generated index will be the biggest integer
index + 1. Note that when two identical index are defined, the last
overwrite the first."
> Your code brought my attention to the fact that I have now and then
> seen syntax that is different from what I have learned from manuals or
> that I have seen on php.net and I wondered if there is some place these
> various alternatives are documented.
Got everything from PHP docs (follow the table of contents to see the
good stuff)
http://php.net/docs.php
> Often it seems that you need to be familiar with these things in order
> to find documentation for them. There is kind of an assumption of
> basic knowledge, especially on pnp.net.
I found that starting out too. What I find super frustrating about PHP
is that there is no stable naming convention enforced! One minute you
are calling htmlentities(), the next, array_merge_recursive(). This
needs to be sorted out because it can be such a pain in the a$$ to
remember.
BTW: Another cool thing I learned a bit late in life about PHP is that
it allows chain assignment of variables:
$a = $b = $c = "test"; // a, b, c all equal the string "test";
You can also assign variables while testing them for a condition:
if(($cnt=count($array))>1){ echo "The count of the array is $cnt"; }
Anyway, hope this helps.
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Navigation:
[Reply to this message]
|