2

I have the following code:

$el = "<div style='padding: $padding'></div>";

The problem is i cannot write

$el = "<div style='padding: $paddingpx'></div>";

because $paddingpx would then be another variable.

And this will produce a wrong css value

$el = "<div style='padding: $padding px'></div>";

because if $padding = 20 then '20 px' is wrong and should be '20px'.

How do i concatenate 'px' to the above code. Note that the variable $padding is a value that i will receive from the $_POST action. I would like to know how i can concatenate 'px' with the padding style value without creating any further lines of code of breaking the string like so.

$el = "<div style='padding: $padding"."px".'></div>";

Thank you, MMK.

2
  • 4
    $el = "<div style='padding: {$padding}px'></div>"; Commented Dec 25, 2014 at 20:37
  • The whole purpose of this question is to address the concatenation problem and put everything in a single line of code without breaking the string. And the answer to it is curly braces around the PHP variable like so "<div style='padding: {$padding}px'></div>"; Thank you all. MMK Commented Dec 25, 2014 at 21:36

4 Answers 4

2

Add a curly brace like

$el = "<div style='padding: {$padding}px'></div>";

Any scalar variable, array element or object property with a string representation can be included via this syntax.

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

Comments

2

Try this:

$el = "<div style='padding: {$padding}px'></div>"; 

1 Comment

@Eugen Is not copy paste man, is the easiest way to do it. You don't invent PHP.
1

You can try

$el = "<div style='padding: ".$padding."px'></div>";

Comments

1

This code works as well.
There is some ways to using php code in other php string that contents are html.

$el = "<div style='padding: ".$padding."px'></div>";

Comments

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.