Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to write good PHP code in dreamweaver?

New Here ,
May 08, 2010 May 08, 2010

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>';
   }
?>
TOPICS
Server side applications
8.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 08, 2010 May 08, 2010

do what worked for you earlier then:

<?php if( $welcome ) { ?>
      <h1>Welcome <?php echo $name; ?></h1>
   <?php } else { ?>
      <h2>Good bye</h2>
   <?php } ?>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2010 May 09, 2010

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 09, 2010 May 09, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 09, 2010 May 09, 2010
LATEST

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/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines