Exploring the world of PHP

Discussion in 'PHP' started by hemasid, Mar 16, 2011.

  1. #1
    can any help me regarding how PHP handles strings. Is there any diffrence when we use a double quotes or single quotes in any program. I tried this small program but get the same output in all the cases.

    $around='around';
    echo "What goes ". $around .",comes ". $around ." <br/>";
    echo 'What goes '. $around .',comes '. $around .'<br/>';
    $around="around";
    echo "What goes ". $around .",comes ". $around ." <br/>";
    echo 'What goes '. $around .',comes '. $around .'<br/>';
     
    hemasid, Mar 16, 2011 IP
  2. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #2
    No there isn't. PHP using weak typing, which means you do not have to define a variable type. However, it does have a presedence order so declaring;

    $myvar = "2"; //This will be counted internally as a char* array or std::string

    is different from this

    $myvar = 2; //This will be counted internally as an interger

    and this

    $myvar = 2.0; //This will be counted as a float (or long on some systems) internally.

    This however does not matter a great deal for your basic n00bie. You will only ever need to learn this stuff if you NEED to know it for PHP. I suggest a quick Google Search in variable declaration and strong and weak typing. Also search for something called "Casting" (Not used in PHP but will help you to understand why you paid so much for that shiny new Intel processor).

    Hope this helps!
    Andrew
     
    awood969, Mar 16, 2011 IP
  3. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    php can substitute variables inside double quotes, try :

    echo "what goes $around, comes around <br>" ;
     
    mallorcahp, Mar 16, 2011 IP
  4. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #4
    mallor thats a very depreciated style. a PHP5/XHTML compliant form would be;

    echo("What goes $around, comes around <br />");

    But yes you can do that. You only ever need to do "something" . $like . "this" when you do "something" . nl2br($like) . "this" if you understand me.
     
    awood969, Mar 16, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes it seems you are using variables like javascript as php does not need the spacing and dots just use the variable once define $around
     
    srisen2, Mar 16, 2011 IP
  6. hemasid

    hemasid Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the help :)
     
    hemasid, Mar 17, 2011 IP
  7. hemasid

    hemasid Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You only ever need to do "something" . $like . "this" when you do "something" . nl2br($like) . "this" if you understand me.

    did not get the last line.....:(

    pls explain
     
    hemasid, Mar 17, 2011 IP
  8. hemasid

    hemasid Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @ Srisen 2: I do not know javascript. Was just trying my hand on concatenation. But java script is the next one in my line "To DO"
     
    hemasid, Mar 17, 2011 IP
  9. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #9
    Double quotes automatically parse ur variable. For instance,

    $var='there';

    //Case 1
    Echo "Hello $var, how are you?" ;

    //Case 2
    Echo "Hello"." ".$var.", how are you?";

    Both cases 1 and 2 will outputs exactly the same result but what made all the differences is the syntax of case 2 which appears alot quite complicated. A good programming is writting out the code neatly, which will eventually prevent any errors from occuring in withing your code.


    nl2br() in php is equivalent to <br/> in html.
     
    spaceman12, Mar 17, 2011 IP
  10. hemasid

    hemasid Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    @spaceman12: Thanks for telling about nl2br(). Was just searching for it.

    Suggest some good source for learning PHP.
     
    hemasid, Mar 17, 2011 IP
  11. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #11
    nl2br() is not the PHP equiv to <br /> in XHTML (not HTML). It is a function that will convert all C style \n keywords into XHTML compliant <br /> tags.

    Also a brilliant book for beginners is on amazon, "PHP5 in easy steps". It's what I used many many moons ago but its very very good at teaching all the core components, leaving you ready to tackle PHP head on. The only thing you should need afterwards is the knowledge of specific PHP functions which can easily be learnt straight from php.net

    Andrew
     
    awood969, Mar 17, 2011 IP
  12. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #12
    as for me, http://www.w3schools.com is where i first started off with php.(now m very and highly advanced,) All the explaination and tutorial there are just too simple and unique. When you have become a lil bit more advanced, php.net, the online manual documentation for php, is where u will most probably be wanting to look up for various functions and their usage related to php.
     
    spaceman12, Mar 17, 2011 IP
  13. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    try looking at some basic examples on php.net youll get it

    variable are

    $variablename = "variable ifnormation";

    and

    echo "$variable";
    to display the variable and just $variable anywhere to use the variable
     
    srisen2, Mar 17, 2011 IP