[nflug] PHP character question

Andrew Bruno aeb at qnot.org
Fri Apr 18 15:06:44 EDT 2008


Just a quick note.. the reason quotes don't appear to have any effect is
because of the way PHP handles "bare strings". I find this to be an
interesting design choice of the language. The PHP manual has a blurb
about it here:

""
PHP automatically converts a bare string (an unquoted string which does
not correspond to any known symbol) into a string which contains the bare
string. For instance, if there is no defined constant named bar, then PHP
will substitute in the string 'bar' and use that.
""

(See: http://www.php.net/manual/en/language.types.array.php under the
section "Array do's and don'ts")

Why they chose to include this behavior in PHP is beyond me. I would
recommend always using quotes around string literals. For example, if you
were to add:
  define('A', 'ur screwed');

to the beginning of your code (or anything included by your code) things
would break.

You might try using the ord() and chr() functions for your solution. This
way the <= test is comparing integers (the ascii value of the char) as
opposed to strings.

    for($i = ord('A'); $i <= ord('Z'); $i++) {
        echo chr($i);

        if($i == ord('M')) {
            echo "\n<br/>\n";
            continue;
        }

        if($i != ord('Z')) {
            echo ' | ';
        }
    }


Cheers,

--Andrew






> Using quotes on the characters didn't appear to have an effect on either
> the
> loop or the if condition, so I left it omitted.  The 2nd piece of code is
> adding a separator in between every letter except for M and Z.  My desired
> output is:
> <http://136.183.201.239/%7Edegerc/index.php?letter=A>
> A | B | C | D | E | F | G | H | I | J | K | L | M
> N | O | P | Q | R | S | T | U | V | W | X | Y | Z
>
>
> Here is my overall code:
>
>
> for ($i=A; $i<Z; $i++)
>   {
>    if ($i === N) { echo "\n\n<br>\n\n"; }
>
>    echo $i;
>
>    if ($i <> M || $i <> Z) { echo " | "; }
>   }
>
>
>
> On Fri, Apr 18, 2008 at 12:40 PM, Robert Meyer <meyer_rm at yahoo.com> wrote:
>
>> Well, a couple of observations...  You never quote any of the characters
>> that you're assigning.  I'm not sure if it's an omission or what.
>>
>> Second, in the chunk of code with the ||, the first piece of code with
>> give a a '|' for *every* character.  For the second, you will get two
>> for
>> every character that is not M or Z and one for each M or Z.
>>
>> Maybe a cut and paste of the whole code fragment might help figure out
>> what's going on.
>>
>> Cheers!
>>
>> Bob
>>
>> --
>> "When once you have tasted flight, you will forever walk the earth with
>> your eyes turned skyward, for there you have been, and there you will
>> always
>> long to return."
>> --Leonardo da Vinci
>>
>>
>> ----- Original Message ----
>> From: Rob Dege <livemotion at gmail.com>
>> To: nflug at nflug.org
>> Sent: Friday, April 18, 2008 12:22:35 PM
>> Subject: [nflug] PHP character question
>>
>> Hello to all,
>>
>> I am trying to create a simple for loop, but instead of incrementing
>> numbers, I want to increment characters.  I am able to get the code to
>> work,
>> up until the end, when it comes time to output the last char.
>>
>> for ($i = A; $i < Z; $i++)
>>   {
>>     echo "$i <br>";
>>    }
>>
>> In this simple example, the output would be to echo the characters A -
>> Y,
>> omitting Z; which is expected.  If I change $i < Z to $i <= Z, one would
>> expect to have Z included in the output.  Unfortunately, this does not
>> happen.  Instead, it continues to loop past Z until it outputs YZ (A -
>> Z, AA
>> - AZ, BA - BZ, ..... YA - YZ).  This would imply that it's matching
>> against
>> ZA instead of just Z.  I know there are alternate ways around this, but
>> now
>> I'm really curious about the looping behavior here.  Any insight is
>> appreciated.
>>
>>
>> I also have a related question with an if condition, if you want further
>> head scratching.
>>
>> if ($i != M || $i != Z) { echo " | "; }
>>
>> With this statement, whenever $i has the value of either M or Z it
>> should
>> execute the condition, but it doesn't.  However, if I separate the if
>> condition into two separate conditions statements, it works as intended.
>>
>> if ($i != M) { echo " | "; }
>>
>> if ($i != Z) { echo " | "; }
>>
>> I'm not sure why the || would cause a problem, but for some reason it
>> does.
>>
>> --
>> -Rob
>>
>> Ben Franklin Quote: "They that can give up essential liberty to obtain a
>> little temporary safety deserve neither liberty nor safety."
>>
>>
>> ------------------------------
>> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try
>> it
>> now.<http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ>
>>
>> _______________________________________________
>> nflug mailing list
>> nflug at nflug.org
>> http://www.nflug.org/mailman/listinfo/nflug
>>
>>
>
>
> --
> -Rob
>
> Ben Franklin Quote: "They that can give up essential liberty to obtain a
> little temporary safety deserve neither liberty nor safety."
> _______________________________________________
> nflug mailing list
> nflug at nflug.org
> http://www.nflug.org/mailman/listinfo/nflug
>




More information about the nflug mailing list