1

I'm trying to build a string of HTML as follows:

$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed)?'':'disabled'.">"; 

But it just gives me a parse error (no specific detail, just that this line is the problem).

I've tried various positioning of quotations and brackets but I'm always getting the parse error. Is this possible the way I'm trying?

6
  • Your line does not produce any parse error, but heed @codingbiz's answer, as the ternary operator in PHP has a very low precedence. Commented Oct 8, 2012 at 14:26
  • @lanzz, you say it doesn't produce a parse error but I've copied the line exactly as it is in my code and I'm still getting Parse error: parse error in <file> on line 201 Commented Oct 8, 2012 at 14:30
  • I have copied and pasted your exact line, and after adding a definitions for the constant and the two variables, the code does not produce any errors; even with the constant and variables undefined, the errors produced are not parse errors. The cause of your error is likely not on this line, PHP's error reporting is notoriously inaccurate. Commented Oct 8, 2012 at 14:32
  • If I comment out this line and this line only, the parse error is gone. Commented Oct 8, 2012 at 14:35
  • This does not help the fact that this line alone is not enough to reproduce your problem. Commented Oct 8, 2012 at 14:36

4 Answers 4

3
$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed?'':'disabled').">"; 

Like codingbiz said, this should work with additional parentheses. I'd go for a more readable version with sprintf though:

$html = sprintf( '<input name="%s" value="%s" size="4"%s>', GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT, $MaxCallRecordingTimeSecs, ( $bCallRecordingLicenced ? '' : ' disabled' ) ); 
Sign up to request clarification or add additional context in comments.

4 Comments

I agree on the sprintf method, thanks. However, this gives a parse error as well :S
@MatthewMcGovern Not to be a smart-ass, but that's rather impossible. Check the line above this one: it probably goes wrong there. What is the exact message you're getting?
Not a smart-ass at all. There's no semi-colon on the line above. I'm going to pretend I never asked this question lol. I thought because it worked fine when I comment out the line in question that the error was on that line. Totally overlooked the line above... Mondays are never easy.
@MatthewMcGovern Tell me about it ;) Glad your problem is solved.
1

Try

".(($bCallRecordingLicensed)?'':'disabled').">"; 

additional brackets

2 Comments

I don't see reason why. If you take this portion out does it work?
the way you had it in the question confused the parser - seems like the ' ' was never completed before concatenating with ." ". Take ternary part out and see if it still works
1

Try wrapping the whole ternary in parens, rather than just the variable at the start:

$html= "<input name='".GROUP_CONFIG_MAX_CALL_RECORDING_TIME_INPUT."' value='".$MaxCallRecordingTimeSecs."' size='4' ".($bCallRecordingLicensed?'':'disabled').">"; 

Comments

-1

I think its because you change quote marks:

For example

$test = false; $strings = "hello ".($test?"you":"")." this is a test"; echo $strings; 

works as you expected.

I did find that without the brackets round the test, for example, my test only produced the word "you" .. rather than the whole string - which was odd.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.