How to use "If then" nested into a string variable

I have been away from PHP for a little while and maybe I have forgotten the basics but here is something like I want to do to Build an XML file.

I am having trouble formatting the “IF” statement in the string so that it works inside of the string variable.

Maybe it is an easy fix and I have just been away too long?

$scannercapabilities='<scan:ScannerCapabilities xmlns:xsi="" xmlns:scan="" xmlns:pwg="" xsi:schemaLocation=" eSCL.xsd">
                                           '.(if ($mode24bitcolor=="yes") echo "<scan:ColorMode>RGB24</scan:ColorMode>";).' 
<.....more xml>';

the problem is the bottom line with $mode24bitcolor variable . Seems no matter how I format it I get an error. the variables in the beginning are fine. php seems to trip up on the “If” statement

It should be possible to build this variable from other vartiables.

I have tried this with and without parenthesis and with and without “.” to concatenate

Using echo during an expression makes no sense; it’s a command to output something. You could use a ternary operator:

<scan:ColorModes>' .
    ($mode24bitcolor == "yes" ? '<scan:ColorMode>RGB24</scan:ColorMode>' : '') .
    '<...more xml>';

Works beautifully , thanks so much. Is this something changed in newer PHP versions?

As near as I can make out, they’ve been available in every version of PHP since pretty early on. I can’t see them in the release notes at all, so at least PHP 3. They’re useful for simple circumstances like this but can get unwieldy quickly if you ask too much of them.

Sponsor our Newsletter | Privacy Policy | Terms of Service