functions and integers in strings

Discussion in 'PHP' started by jumpenjuhosaphat, Dec 9, 2006.

  1. #1
    Okay, I have 2 questions.

    First: When using a function within a string do I need to place the entire function within ()parenthesis?

    echo 'this is what I mean'.ceil($num/4);
    or
    echo 'this is what I mean'.(ceil($num/4);

    The second question is even though PHP doesn't require variables to be declared as integer or string or etc, can I mix them when concantonating a string, like in the above examples? Or do I have to convert the digit into a string first?
     
    jumpenjuhosaphat, Dec 9, 2006 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    first example is correct, and variables can be whatever, php is pretty clever for the most part, typically though int would be defined without quotation marks of any sort $var = 2;

    hope that helps.....
     
    krakjoe, Dec 9, 2006 IP
    jumpenjuhosaphat likes this.
  3. jumpenjuhosaphat

    jumpenjuhosaphat Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That does help. Thank you. Rep added.
     
    jumpenjuhosaphat, Dec 9, 2006 IP
  4. jumpenjuhosaphat

    jumpenjuhosaphat Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One question that I forgot to ask, when doing math within a string, do you surround that with ()parenthesis?

    a$=b$.(c$+1);
    or
    a$=b$.c$+1;
     
    jumpenjuhosaphat, Dec 9, 2006 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    urm neither of them are correct, to do maths with strings :

    
    <?
    $a = 4;
    $b = 8;
    $c = 3;
    $e = 15;
    
    $sum = ($a * $b) + $c;
    
    // $sum = (4 * 8) + 3
    // echo $sum : output 35
    
    $sum = ( $c + $a ) * $b;
    
    // $sum = ( 3 + 4 ) * 8
    // echo $sum : output 56
    ?>
    
    PHP:
    got it ?
     
    krakjoe, Dec 9, 2006 IP
  6. jumpenjuhosaphat

    jumpenjuhosaphat Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What I mean, is in this case:

    $b='String_Text';
    $c=3;
    $a=$b.$c+3;

    or
    $b='String_Text';
    $c=3;
    $a=$b.($c+3);

    So the result I'm trying to get is "String_Text6"
    I want to concantonate the integer 6 to the end of the string "String_Text"
     
    jumpenjuhosaphat, Dec 9, 2006 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
    <?
    /*
     PHP will spit errors if you try to use the code you have above, or not errors
     but not numbers you can use
     Instead, use something like the code below
    */
    $str = "String_Text";
    $a = 2;
    $b = 3;
    
    echo $str . ( $a * $b );
    ?>
    
    PHP:
    yeah, ou had it right in the second one you did, which I only just read properly, sorry, you got it....
     
    krakjoe, Dec 9, 2006 IP