Copy link to clipboard
Copied
Here's the thing. I'm using Dreamweaver CS4 to develop a website. In the past my php files would look like this:
<?php
echo '<h1>Welcome ' . $name . '</h1>';
?>
This caused problems in the design view because all I would see would be the php icon to let me know I have some script in this location and that is it.
Then I started to realize that I should write my code like this:
<h1>Welcome <?php echo $name; ?></h1>
This second way of writing php code is a lot easier to work with in Dreamweaver since in the design view I can see the formatted h1 tag with the word 'Welcome' beside it (followed by the php icon). I can still apply all the standard CSS rules inside Dreamweaver and the design view updates nicely.
So now here is my issue.What is the best way to handle if statements? Say I want to toggle between <h1>Welcome $user</h1> and <h2>Good bye</h2> BUT I would like to still be able to format the CSS and see the results on the screen in design view. Is it possible?
If I do the following, then I'm back to square one. I can't see my CSS rules applied in the design view since all I have is the PHP icon to show where the script is located:
<?php
if( $welcome ) {
echo '<h1>Welcome ' . $name . '</h1>';
}
else {
echo '<h2>Good bye</h2>';
}
?>
Copy link to clipboard
Copied
do what worked for you earlier then:
<?php if( $welcome ) { ?>
<h1>Welcome <?php echo $name; ?></h1>
<?php } else { ?>
<h2>Good bye</h2>
<?php } ?>
Copy link to clipboard
Copied
Thanks iPHP, this is exactly what I was looking for. I didn't realize that you can break up if statements this way and have them still work!
Copy link to clipboard
Copied
php code didn't apear in design view
you should click live view or right click on file name and preview in browser
hope that help u
Copy link to clipboard
Copied
There is another way. But it's considered advanced PHP in this community. It's called a a ternary operator. So
<?php if( $welcome ) { echo '<h1>Welcome ' . $name . '</h1>'; } else { echo '<h2>Good bye</h2>'; } ?>
woud look like:
<?php echo($welcome?'<h1>Welcome '. $name . '</h1>' : '<h2>Good bye</h2>'); ?>
Here's a link to a good explanation: http://www.addedbytes.com/code/ternary-conditionals/