Percents can be really tricky ones



  • The same PHP forum (see my earlier post) never seems to be running out of great logical ideas. Recently one happy coder (not the same as in the last post) asked if there could be easier way to add 17,5% to total sum. Currently he/she was using this beatiful construction:

    $ai = ($price*$quantity);
    $bi = ($ai/'10');
    $ci = ($bi/'2');
    $di = ($ci/'2');
    $ei = ($ai+$bi+$ci+$di);

    Couldn't belive my eyes. But, it surely is correct.



  • And the fact that it is in PHP makes it look 10x worse[:(]



  • And notice the ()'s and ''s

    This is one example of beauty/ugliness of PHP. Your trying your best to claim that "10 is a char" and still it happily makes the division correctly.



  • The fact that in PHP, you can't just write out the property name of a map, but are forced to use the string notation (eg. $object['property']) may lead some PHP novices to believe that ints also can't be written literally. But still, it'd take a complete absence of data type knowledge to try and DIVIDE BY A STRING.

    Now that I'm in a PHP syntax bashing mood:

    $total = $price*$quantity*$1.175

    It's subtle.



  • It's just me, but I hate languages where you have to prefix variables with a $, it hurts my eyes and brain, I find it very hard to read.
    The $'s serve no use and make it harder to read.



  • @t-bone said:

    It's just me, but I hate languages where you have to prefix variables with a $, it hurts my eyes and brain, I find it very hard to read.
    The $'s serve no use and make it harder to read.


    In PHP they serve no use.

    But PHP is inspired by Perl, in which you had $, #, @ and whatnot to enforce data types. As far as I know. Correct me if I'm wrong. They chucked the strong typing but somewhy kept the $. Then, they invented magic quotes so that the $ would still have a pseudo-use.



  • @dhromed said:


    But PHP is inspired by Perl


    You just made me never, ever want to take on a PHP project.



  • @dhromed said:

    @t-bone said:
    It's just me, but I hate languages where you have to prefix variables with a $, it hurts my eyes and brain, I find it very hard to read.
    The $'s serve no use and make it harder to read.


    In PHP they serve no use.

    But PHP is inspired by Perl, in which you had $, #, @ and whatnot to enforce data types. As far as I know. Correct me if I'm wrong. They chucked the strong typing but somewhy kept the $. Then, they invented magic quotes so that the $ would still have a pseudo-use.


    I did mean to say, in php they serve no use, there is indeed perl or some python preparsing extension stuff and things that do something useful, but still most of the times other implementations where available and it still hurts readability

    If I have to look at code for 10 hours a day I want the syntax to look easy on the eyes



  • @dhromed said:

    Now that I'm in a PHP syntax bashing mood:
    $total = $price*$quantity*$1.175
    It's subtle.


    Parse error: parse error, unexpected T_DNUMBER, expecting T_VARIABLE or '$' in - on line 1

    Pretty easy to spot at run time.



  • He meant:

    $total = $price * $quantity * 1.175;



  • @Otac0n said:

    He meant:
    $total = $price * $quantity * 1.175;
    Yeah i know what will compile ( err parse ), but i thought he was poking fun at PHP's '$' as a varible prefix and '$' as in dollars.



  • @paranoidgeek said:

    @Otac0n said:
    He meant:
    $total = $price * $quantity * 1.175;
    Yeah i know what will compile ( err parse ), but i thought he was poking fun at PHP's '$' as a varible prefix and '$' as in dollars.


    YOU ARE CORRECT

    DING DING DING DING

    :)



  • WHAT?

    PHP doesn't handle dollar amounts????

    Well, I guess I'm going back to COBOL.NET then. [I]



  • @mrprogguy said:

    WHAT?

    PHP doesn't handle dollar amounts????

    Well, I guess I'm going back to COBOL.NET then. [I]



    Switchboard.NET


  • Brainf*k.net



  • I really want to write AWK #(1). (pref with the compiler in Java).



    Alas I am lazy.


    1. Pronouced Awk as in Awkward, # as in Cannabis.

      [pi]


  • @t-bone said:

    @dhromed said:
    @t-bone said:
    It's just me, but I hate languages where you have to prefix variables with a $, it hurts my eyes and brain, I find it very hard to read.
    The $'s serve no use and make it harder to read.


    In PHP they serve no use.

    But PHP is inspired by Perl, in which you had $, #, @ and whatnot to enforce data types. As far as I know. Correct me if I'm wrong. They chucked the strong typing but somewhy kept the $. Then, they invented magic quotes so that the $ would still have a pseudo-use.


    I did mean to say, in php they serve no use, there is indeed perl or some python preparsing extension stuff and things that do something useful, but still most of the times other implementations where available and it still hurts readability

    If I have to look at code for 10 hours a day I want the syntax to look easy on the eyes

    The only 1-char-prefix I know of in Python (other than the "_" "__" which don't mean much) is "@", used for function decorators (basically a function that takes a function and returns one. Strange, but quite poweful magic)

    The language I'd use for meaningful character prefix would probably be ruby though, apart from the $whatever global constants (which are ugly), I really like defining symbol with ":symbol" and accessing an object's attributes by "@attr" (which is equivalent to java's "this.attr" also note that using "self.attr" in Ruby doesn't actually access and attribute of "self", it calls the "attr" method of the "self" object. And no, you can't access an object attribute from outside of the object in ruby), it's clear, readable, and even not liking prefixes much I found that I didn't have any actual issue against these.



  • @paranoidgeek said:

    @Otac0n said:
    He meant:
    $total = $price * $quantity * 1.175;
    Yeah i know what will compile ( err parse ), but i thought he was poking fun at PHP's '$' as a varible prefix and '$' as in dollars.


    wait a second, i think the original syntax will work. '.' serves as a concatination operator, so it will try to multiply three variables: 'price', 'quantity', and '1', and then it will concatinate that with 175. no?



  • @mickeyreiss said:

    @paranoidgeek said:
    @Otac0n said:
    He meant:
    $total = $price * $quantity * 1.175;
    Yeah i know what will compile ( err parse ), but i thought he was poking fun at PHP's '$' as a varible prefix and '$' as in dollars.


    wait a second, i think the original syntax will work. '.' serves as a concatination operator, so it will try to multiply three variables: 'price', 'quantity', and '1', and then it will concatinate that with 175. no?

    Indeed, if $1 were a legal variable name (and defined), it would. $s1.175 would do what you said (175 being treated as a 'bare string', another goofy relic trapped in php).



  • @foxyshadis said:

    @mickeyreiss said:


    wait a second, i think the original syntax will work. '.' serves as a concatination operator, so it will try to multiply three variables: 'price', 'quantity', and '1', and then it will concatinate that with 175. no?

    Indeed, if $1 were a legal variable name (and defined), it would. $s1.175 would do what you said (175 being treated as a 'bare string', another goofy relic trapped in php).


    Are you sure it's a bare string instead of an int literal being auto-converted to string due to PHP's weak typing?


Log in to reply