Page 1 of 3 123 Last
  • Jump to page:
    #1

  1. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    Red face Getting Error In Small Function.


    Hi Friends I am newbie and learning php.
    I am getting error in variables.
    please check it and define
    1. What is error
    2. How to resolve it
    3. How to use that function properly


    PHP Code:

                        <?php $a=100;
                                
    $b=50;
                                
    $c=50;
                                
    $a+$b+$c=$d;
                                echo 
    "$d = is the right answer." ?>
    One More Code
    PHP Code:


                          <?php echo "<b>What is Your Name? <br />";
                                
    $name=100Here I have Error
                                $name
    =samee;
                                echo 
    "My Name is $name." ?>
    In 2nd , samee should replace by 100, but it is giving error. Why?

    Please Check Last 2 functions; I made comment there.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    724
    Rep Power
    7
    $name = 100; This gives you an error?
    $name = samee; Best to offer string values in quotes...
    $name = 'samee';
    $name = 100; and $name = '100'; are not the same.
    100 is an integer, while '100' is a string.

    Is the $name = 100; really the line w/ the error?
    Last edited by Triple_Nothing; September 26th, 2013 at 08:36 AM.
    He who knows not and knows not he knows not: he is a fool - shun him. He who knows not and knows he knows not: he is simple - teach him. He who knows and knows not he knows: he is asleep - wake him. He who knows and knows he knows: he is wise - follow him
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    189
    Rep Power
    84
    Originally Posted by same2cool

    PHP Code:

                        <?php $a=100;
                                
    $b=50;
                                
    $c=50;
                                
    $a+$b+$c=$d;
                                echo 
    "$d = is the right answer." ?>
    Read about assignment operators here http://au1.php.net/manual/en/language.operators.assignment.php and you'll see why the echo line doesn't give you what you expect.
    It really means that the left operand gets set to the value of the expression on the right.
  6. #4

  7. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    Wink


    I also asked from my teacher about it.
    he said,

    variable is always written at left. like

    PHP Code:
    $d=$a+$b+$c 
    by this way I can resolve my problem.

    and 2nd problem you have resolved ; like

    string is always written with in double quotes.

    like
    PHP Code:
    $name="Sami" 
    thanks for help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,031
    Rep Power
    377
    $name="Sami" or $name='Sami' is same.

    using ' ' instead of " " is MARGINALLY faster
  10. #6

  11. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    Good Point


    Originally Posted by paulh1983
    $name="Sami" or $name='Sami' is same.

    using ' ' instead of " " is MARGINALLY faster
    It is a good point. hat off.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by paulh1983
    using ' ' instead of " " is MARGINALLY faster
    Much more importantly: Single quoted strings are just literal strings, whereas double quoted strings evaluate variables and special sequences like \n. This often leads to unexpected behaviour if people aren't familiar with the language.

    Do not use double quotes unless you have a specific reason for using them.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8

  15. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    Wink Hi Sir


    Originally Posted by Jacques1
    \n.
    what is it for?

    My teacher still did not tell me about this?

    Originally Posted by Jacques1
    Do not use double quotes unless you have a specific reason for using them.
    hmmmmmmmm, I am using double quotes mostly, because in this we can use variables easily. ; but Now i'll try to use single quotes from next time.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    724
    Rep Power
    7
    Single is the best, and requires you to have more control of your code. Double, as I would word it, runs a bit more loosely, because it allows more processing versus just an echo/printout.
    Code:
    echo "You have $vacDays vacation days remaining.<br />\n";
    echo 'You have $vacDays vacation days remaining.<br />\n';
    As far as what the user will see on the page (not in the code) will differ as such. If $vacDays is set to 12, the first will appear to the user as:
    You have 12 vacation days remaining.
    While the second will appear as:
    You have $vacDays vacation days remaining.\n

    The <br /> will not appear simply because it is HTML code and processed by the browser.
    The \n is meerly a newline comment to PHP so it enters a carriage return in the code before the next output occurs.
    One way to correct the second line would be:
    Code:
    echo 'You have ' . $vacDays . ' vacations days remaining.<br />' . "\n";
    The \n still needs to be in double quotes so it will process. You can also manually enter a return yourself before you enter your closing character, as well as:
    Code:
    echo 'You have ' . $vacDays . ' vacations days remaining.<br />' . PHP_EOL;
    Last edited by Triple_Nothing; September 26th, 2013 at 06:04 PM.
    He who knows not and knows not he knows not: he is a fool - shun him. He who knows not and knows he knows not: he is simple - teach him. He who knows and knows not he knows: he is asleep - wake him. He who knows and knows he knows: he is wise - follow him
  18. #10
  19. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    I had read somewhere that later versions of PHP (5.3+) had optimised the double quote so much that it was faster than the use of single quotes...in looking for that as a reference I found a much more interested article:

    Disproving the Single Quotes Performance Myth

    As an aside, why does seemingy everyone (apart from me) insist on using double quotes for HTML attributes? Single quotes work just as well, are cleaner to read and faster to type..and I've never seen a validation error refer to their use....by using single quotes fro HTML your PHP can wrap the string in double quotes and you don't have to escape " all the time
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  20. #11

  21. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    yes sir


    Originally Posted by Northie
    I had read somewhere that later versions of PHP (5.3+) had optimised the double quote so much that it was faster than the use of single quotes...in looking for that as a reference I found a much more interested article:
    Nice to inform sir.

    i'll remember it. now.
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    What you should remember is that micro-optimizations are nonsense.

    Spend your time on readability, robustness and security. Not on questionable "optimizations" that may or may not save a few nanoseconds.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  24. #13

  25. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    good sir


    Originally Posted by Jacques1
    that may or may not save a few nanoseconds.
    that is also a great point sir.
  26. #14

  27. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    2

    \n escape sequence using


    Originally Posted by Triple_Nothing
    it allows more processing versus just an echo/printout.
    I Don't know more about it. still my teacher did not tell me about printout command.

    Originally Posted by Triple_Nothing
    Code:
    echo "You have $vacDays vacation days remaining.<br />\n";
    echo 'You have $vacDays vacation days remaining.<br />\n';
    May be you are right, I have found the solution of my error but. here I had also one error in my small code that is not working i don't know why.

    PHP Code:
    <?php  
                              $name1
    ="Samee";  
                              
    $name2="Ullah";  
                              
    $name3="Feroz";  
                              echo 
    "$name1 $name2\n$name3 <br />"//Error Here Sequence is not working.  
                              
    echo "$name1 $name2\t$name3 <br />";  
                              echo 
    "$name1 $name2\r$name3";  
                         
    ?>

    In this code Escape Sequence is not working.

    I showed output in my Escape Sequence post in my blog.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    /root
    Posts
    14
    Rep Power
    0

    Thumbs down


    Double quotes are also one cause for XSS - directly putting the variable into a string like "$myVar" can lead to remote code execution, in some rare cases...but we dont want that, even if its rare.

    To the code:
    PHP Code:
    <?php 
    $a
    =100
    $b=50
    $c=50
    $a+$b+$c=$d
    echo 
    "$d = is the right answer." 
    ?>
    It probably got explained already, however, maybe some point-blank readings in the code are a help for others.

    PHP Code:
    <?php 
    $a
    =100// Is correct.
    $b=50;  // Correct too
    $c=50;  // Correct a swell.
    $a+$b+$c=$d// Not correct at all. Like in algebra, the targed to be defined is on the beginning - well not always in algebra, but surely always in PHP and any other coding language. Therefore it should be:
    $d=$a+$b+$c;
    echo 
    "$d = is the right answer." // You forgot the last semi-colon - and are using double quotes as it has been pointed out already. In reality, this may be:
    echo $d.' = is the right answer.';
    // You see the .? it means you are connecting the variable to the string. IMHO, this is the safest way to go.
    ?>
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo