1

I'm currently trying to put two variables together, one that has already a specific number automatically calculated by imagesy and the second variable is returning data from url with $_GET['l2_y']

imagettfstroketext($jpg_image, 0, $y . $options['text']['font-position']['l2_y'] );

The $y returns '29'

and the $options['text']['font-position']['l2_y'] returns +150 (or whatever I set in the header)

I am trying to combine the two but it's not working at all, if I remove the $y . then the +150 works but if I add the . it doesn't calculate everything..

I've been stuck at this for a while now, I'm not sure how to combine the two, the reason I am using + signs in get is because it can also be used as a -150 to change the position of the text.

4
  • 1
    Combining as is adding the two numbers up? Commented Mar 29, 2016 at 15:48
  • 1
    How about something like intval($y) + intval($options['text']['font-position']['l2_y']) to coerce both to numbers (they might be strings now) and then simply use mathematics (and not the . concatenation)? Commented Mar 29, 2016 at 15:50
  • 1
    @somethinghere that's actually perfect, if you'd like to add that as the answer I'd appreciate it. Commented Mar 29, 2016 at 15:52
  • I've added it as an answer. Commented Mar 29, 2016 at 15:55

4 Answers 4

7

You could try to coerce both numbers to integers using intval and then add them (using + and not ., which is used for concatenating strings):

intval($y) + intval($options['text']['font-position']['l2_y'])

That way you are sure both are numbers before you add them (so you avoid any unwanted results).

However, it is not necessary to coerce them into integers if you simply use the + (adding) operator and not the . concatenation operator, as the following works as well:

echo '+2' + 3; // outputs 5

PHP automatically coerces numbers, intval is useful if you want to be sure it is a number (usually when somebody inputs a number into your site).

Sign up to request clarification or add additional context in comments.

Comments

2

Here you go:

<?php
    $var1 = '1';
    $var2 = '+1';
    $var3 = '-1';
    
    echo $var1 + $var2; //2
    echo $var1 + $var3; //0
?>

2 Comments

Why should the OP try this? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
You are right but I think in here it is so easy to understand .. here the code explains itself ... if you try to explain some thing so simple i think it will look complex
1

You shouldn't be using the . operator, this will concatenate the values as if they were strings. The correct way to do this would require the use of +.

For example: imagettfstroketext($jpg_image, 0, $y + $options['text']['font-position']['l2_y'] ); assuming that $y is a number and not a string.

Using the values you have, this will return the number 179, whereas the version using the . operator will return a string which contains the value "29150"

If the values are not numbers and are in fact strings, you can also encapsulate each variable in an intval($var) which will take the integer value of the variable if it exists.

Comments

1

I think this is what you're after:

imagettfstroketext($jpg_image, 0, intval($y) + intval($options['text']['font-position']['l2_y']));

. is for strings, + is for numbers. In case $y and/or $options['text']['font-position']['l2_y'] are strings (which they are if they have quotes around them), cast them to integers with intval(). PHP will interpret +150 as 150 and -150 as -150

4 Comments

Please do not ask questions in answers.
I changed it but out of curiosity; why?
Because an answer is not designed to be a question, especially if the question is overly broad. Stack Overflow is designed to be a question/answer site. The OP asks the question, others answer. If you need to ask a clarification question you should ask it in comments.
@JayBlanchard It was intended to convey my doubt as to whether or not it could really be so simple (I rarely give a one line answer). Anyway, no biggie. "I think" is an adequate substitute... I think :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.