PHP Question {SOLVED}

7 replies
Curious I was attempting to out put an image in an IF statement based on a few condidtions then I thought an easier way would be if I could store the image in a VAR if that statement was true - allowing me to echo out the VAR (image) later and where I wanted it..

so I thought maybe something like
$img = <img ...>;

but that didn't work anyone know of something I can do here
#php #question
  • Profile picture of the author BlueLayerHost
    Why can't you use an if statement or a switch?

    You need to give more information on what you're trying to accomplish.
    BlueLayerHost - Shared + Managed VPS Hosting
    BlueLayerMedia - Web Development
    WPMalware - Resource for WordPress Seurity + Exploits
    {{ DiscussionBoard.errors[5807443].message }}
    • Profile picture of the author kdm5157
      HTML is text, so you're wanting to store text in a PHP variable for future use (if I'm understanding you correctly). Text are stored as STRING variables and string literals (meaning, use EXACTLY WHAT IS TYPED) need to be surrounded by quotes - single ' or double " both work, you just have to be consistent.

      I suggest using DOUBLE QUOTES " for HTML and SINGLE QUOTES ' for PHP. If you are consistent then you don't have to remember how to nest them properly, just know to use ' when doing PHP and " when doing HTML.

      You want to do this (without the space after the $, it kept removing the variable when I didn't use it):
      PHP Code:
       img '<img src="filename.jpg" alt="Alt Text" title="Title Text" style="width: ##px; height: ##px;" />' 
      I'm suggesting that you always provide an ALT and TITLE attribute for your IMG tags. This is not only good for SEO, but also for standards compliance. Many don't care about that, but - I do lol. HTML is all about following rules.

      HINT: I also put the STYLE attribute with a width and height. If you include those on all of your image tags then you are forewarning the browser to reserve a specified space for an image. Then your design doesn't shift when the images are finally loaded. I hate when sites shift during loading. That's mostly personal preference but thought I'd throw that tid-bit in there since we are talking about images
      {{ DiscussionBoard.errors[5807747].message }}
  • Profile picture of the author solidsoul
    Thank You, I actually had a sleep session last night after I posted this and realized what could be done to accomplish what I was looking to do... although I came up with the same solution that was kinda a "DOH" moment I like you adition to adding the extra feilds..

    here's a questions though, one thing always confused me was when to use " and when to use ' so your saying html " and php ' is that true all across the board or are there cases in that it would be different..

    cause if I'm not mistaken when echo lets say a VAR you don't need anything

    echo $varchar;
    echo '$varchar'; <also workds>
    echo "$varchar";

    wouldn't all three of this work?
    {{ DiscussionBoard.errors[5809560].message }}
    • Profile picture of the author kdm5157
      All 3 of those will absolutely work in PHP. As long as you're consistent in your use, any of those options work. " and ' are interchangeable from a conceptual view.

      Now, using the variable as you did basically means it will translate to a string. If it was not a variable, you would need quotes. That's what's known as a string literal. Examples:

      echo "John";


      echo 'John';

      Those both work, but

      echo John;

      will not.

      You DO NOT have to use " or ' at a specific time, it's just a matter of personal style or preference. Consistency is key.

      echo "John';

      will not work either. Note the double quite opener and single quote closer.

      Feel free to let me know any confusion!
      {{ DiscussionBoard.errors[5809737].message }}
      • Profile picture of the author Earnie Boyd
        Originally Posted by kdm5157 View Post

        You DO NOT have to use " or ' at a specific time, it's just a matter of personal style or preference. Consistency is key.
        If you use " you can combine variable and text data.

        $who = 'John';

        echo "I am writing to $who to let him know I have received his letter.";


        I am writing John to let him know I have received his letter.

        echo 'I am writing to $who to let him know I have received his letter.';


        I am writing to $who to let him know I have received his letter.
        {{ DiscussionBoard.errors[5811127].message }}
        • Profile picture of the author kdm5157
          And to add to that, double quotes are needed if programming line feeds "\r\n". Say, for an email, for example. I thought of these after posting. I felt a little bad for bombarding with info that wasn't really asked for :-/
          {{ DiscussionBoard.errors[5811723].message }}
  • Profile picture of the author solidsoul
    I see what your saying - I made it a note for me to use ' also in php as I was getting confused when it came to echo and html content using " stops the echo so for ME i'm been written html longer than php so It was a real change to use the ' rather than " when echoing html out.. thanks for your input much appreciated -
    {{ DiscussionBoard.errors[5809937].message }}

Trending Topics