How do i start learning PHP

Discussion in 'Software' started by aditichauhan, Jan 21, 2009.

  1. aditichauhan

    aditichauhan Private E-2

    How do i start learning PHP.

    I want to learn PHP.
    From where do i start.
     
  2. stardestroyer

    stardestroyer Private E-2

    Obviously you can buy a book or browse the Internet. Other ways you can start;

    1. setup your PC as a web server. best with XP, IIS and download PHP 6. with this you can copy code from the internet and view it on your PC. But while you are connected to the Internet others can use your PC unless you protect it.

    2. Find a hosting service that provides PHP and databases(optional) tools. I use http://www.dailyrazor.com which charge a low rate but then best not use this commercially as they charge extra for high bandwidth. You will need to buy a domain to go with it which cost from £2.99 from 1and1.co.uk
     
  3. aditichauhan

    aditichauhan Private E-2

    Is there any site/e-book from where i get start from the beginning.
     
  4. stardestroyer

    stardestroyer Private E-2

  5. channerhewitt

    channerhewitt Private E-2

  6. peteschulte

    peteschulte Private E-2

    This is exactly what you want. It will keep you learning for a couple days or a week. You'll get a good foundation. I like W3Schools, but I find Tizag has more comprehensive tutorials.
     
  7. Caliban

    Caliban I don't need no steenkin' title!

    Greetings, peteschulte.

    Good link, but bear in mind that the original post in this thread is dated 01-21-09 and the last reply is dated 02-01-09. ;)
     
  8. peteschulte

    peteschulte Private E-2

    Occam's razor put to good use.

    I would like to say I'll be more observant. At least I won't overlook this factor

    while "the isle is full of noises,
    Sounds, and sweet airs, that give delight and hurt not.
    Sometimes a thousand twangling instruments
    Will hum about mine ears; and sometime voices
    That, if I then had waked after long sleep,
    Will make me sleep again; and then in dreaming,
    The clouds methought would open, and show riches
    Ready to drop upon me, that when I waked
    I cried to dream again." Tempest, Act 3, Scene 2

    Nice!
    How's about this one?
    http://stackoverflow.com/questions/5824090/learning-ajax-how-to-php-connect-to-google-suggest
    Actually, I just posted it on the Google Forum, so maybe next month I'll be getting an insight. Har!
    MG has living persons on it. Cool
     
  9. PC-XT

    PC-XT Master Sergeant

    Setting the user agent didn't work? I think Google prefers dealing with browsers directly rather than through a PHP proxy...
     
  10. peteschulte

    peteschulte Private E-2

    Hi PC-XT,
    I think that you are referring to the thread I referenced
    http://stackoverflow.com/questions/1239601/php-fopen-fails-in-response which includes the following:

    "If it turns out you are having user agent string problems, you can set the user-agent php uses by creating and running a file with the following code in it: <?php ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1) Gecko/20090615 Firefox/3.5'); ?>

    "I think you'll have to restart IIS after setting this, but not 100% on that.

    "Note: this is a random user agent string I pulled, there are many out there, you can set it to pretty much anything you want. There are many more at: http://www.useragentstring.com/"

    Though I read about user_agent on Wikipedia, I don't see what is going on here. Let's see if I got it right, or please advise (and thanks!)
    • It starts with an "If" which I don't know how to test.
    • Assume it is user agent string problems and
    • Google prefers access by a browser rather than by PHP.
    • Then I create a PHP file on my server with the content
      PHP:
      <?php ini_set('user_agent''Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1) Gecko/20090615 Firefox/3.5'); ?>
    • Then access that file in my browser, "creating and running a file."
    • Does this change how all outgoing PHP URL connections from my site appear?
    • Do I leave the original PHP file as it is?
    • Can I ignore the part about IIS?

    Well, it feels good to be tracking down a possible problem and learning!
     
  11. PC-XT

    PC-XT Master Sergeant

    It might actually be better to do this in a new topic if it's getting complicated...

    From the PHP manual for ini_set:
     
  12. peteschulte

    peteschulte Private E-2

    To try one thing, it would be to put the snippet below into the existing PHP script and leave everything else the same.

    PHP:
    <?php ini_set('user_agent''Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1) Gecko/20090615 Firefox/3.5'); ?>
    Right? After your response, I'll give it a shot.
    Then consider whether to create a thread.

    Thanks again!
     
  13. peteschulte

    peteschulte Private E-2

    No joy.
    I think that Google has changed the structure of the return values.
    Here's my new try:

    PHP:
    <?php

        ini_set
    ('user_agent''Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1) Gecko/20090615 Firefox/3.5');
     
      
    $filehandle 
        
    fopen("http://www.google.com/complete/search?hl=en&js=true&qu=" $_GET["qu"], "r"); 
      while (!
    feof($filehandle)){
        
    $download fgets($filehandle);
        echo 
    $download;
      }
      
    fclose($filehandle);
    ?>
    A new thread would be great, thank you!
     
    Last edited: Apr 28, 2011
  14. PC-XT

    PC-XT Master Sergeant

    It appears to be working for me...

    I tried the code without the ini_set on a free host, with these results:

    qu undefined:
    window.google.ac.h(["",[],{}])

    qu=h:
    window.google.ac.h(["h",[["hotmail","","0"],["hulu","","1"],["home depot","","2"],["hyvee","","3"],["hobby lobby","","4"],["hp","","5"],["hgtv","","6"],["hen house","","7"],["honda","","8"],["hr block","","9"]],{}])

    qu=hi:
    window.google.ac.h(["hi",[["history channel","","0"],["hilton","","1"],["hillcrest bank","","2"],["hitler","","3"],["hilton honors","","4"],["hickman mills school district","","5"],["highland community college","","6"],["hipster","","7"],["hi5","","8"],["hiv","","9"]],{}])
     
  15. peteschulte

    peteschulte Private E-2

    Glad you're getting results! How are you viewing them?

    I'm set up to view them on the HTML page that follows. In particular, if you down arrow 63 times starting on the <HTML> line, you'll see where the results are formatted for display on the web page.

    <html>
    <head>

    <title>Google Suggest</title>

    <style>
    #targetDiv {
    background-color: #FFAAAA;
    width: 40%;
    }
    </style>

    <script language = "javascript">
    function getData(dataSource)
    {
    var XMLHttpRequestObject = false;

    if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", dataSource);

    XMLHttpRequestObject.onreadystatechange = function()
    {
    if (XMLHttpRequestObject.readyState == 4 &&
    XMLHttpRequestObject.status == 200) {
    var text = XMLHttpRequestObject.responseText.replace("window.google.ac.Suggest_apply", "callback");
    eval(text);
    }
    }

    XMLHttpRequestObject.send(null);
    }
    }

    function askGoogleSuggest()
    {
    var input = document.getElementById("textField");

    if (input.value) {
    getData("google.php?qu=" + input.value);
    }
    else {
    var targetDiv = document.getElementById("targetDiv");

    targetDiv.innerHTML = "<div></div>";
    }
    }


    function callback(unusedVariable, searchTerm, arrayTerm,
    unusedArray)
    {
    var data = "<table>";
    var loopIndex;

    for (loopIndex = 1; loopIndex < arrayTerm.length;
    loopIndex++) {
    data += "<tr><td>" +
    "<a href='http://www.google.com/search?q=" +
    arrayTerm[loopIndex] + "'>" + arrayTerm[loopIndex] +
    '</a></td><td>' + arrayTerm[++loopIndex] + "</td></tr>";
    }

    data += "</table>";

    var targetDiv = document.getElementById("targetDiv");

    targetDiv.innerHTML = data;
    }
    </script>

    </head>

    <body>

    <H1>Handling Google Suggest</H1>

    Search for <input id = "textField" type = "text"
    name = "textField" onkeyup = "askGoogleSuggest()">

    <div id = "targetDiv"><div></div></div>

    </body>

    </html>


    Or view it live here
     
    Last edited: Apr 28, 2011
  16. peteschulte

    peteschulte Private E-2

    PC-XT
    It appears that your results would fit in the output loop in my code, that I got from the textbook.

    I was concerned that since the book's publishing, Google had changed their output. The numbers used to be huge, reflecting all the links available to say, Amazon or whatever the suggest result was. Now they're just a sequence, but still in the same structure.
     
  17. peteschulte

    peteschulte Private E-2

    Good suggestion, to ask the mods to move this into a thread of its own.

    Your results

    would be accessible on line 31 of my code where we insert the callback function

    Why replace window.google.ac with a callback function? Steve Holzner, who wrote this code and text book says
    I trust that his callback function works, but there might be a hack where I could just echo the output to the page, to see whether there are any results in the XMLHttpRequestObject. This leads to the question, where are you seeing the following? Not on a web page?

    Thanks for wading through all this!!​
     
  18. PC-XT

    PC-XT Master Sergeant

    I was seeing that output when I called the php file directly, by typing it's location in the address bar.

    For some reason google replaced "window.google.ac.Suggest_apply" with "window.google.ac.h", so the code would be changed to
    Code:
    var text = XMLHttpRequestObject.responseText.replace("window.google.ac.h", "callback");
    eval(text);
    It might be better to use regular expressions, in case they change it again, later:
    Code:
    var text = XMLHttpRequestObject.responseText.replace(/window\.google\.ac\..*\(/, "callback(");
    eval(text);
    Try using this last code.
     
  19. peteschulte

    peteschulte Private E-2

    It's bedtime now, just tried typing into the address bar the location of my php file
    http://ajax.matalasco.com/google.php
    with result
    then
    http://ajax.matalasco.com/google.php?qu=a
    with result
    So thanks much! I'm right next to the bed and dear wife has to work early so will sign off on this positive note. More focus to think about.
    I've been suspecting whether the HTML page is successfully calling the script, but I can see them side-by-side in the directory and the syntax looks right. Even thought of giving an absolute or http address to the call to php.

    More tomorrow :)
     
  20. PC-XT

    PC-XT Master Sergeant

    If you open the error console, a.k.a. javascript console, you can see if errors show up when you load the page. If you see some error with window not having a google object or other window.google.ac.Suggest_Apply() error, try changing line 31 to:
    Code:
    var text = XMLHttpRequestObject.responseText.replace(/window\.google\.ac\..*\(/, "callback(");
    (You will probably see warnings, too, like loading a non-XML document, but we're just interested in errors.)
     
    Last edited: Apr 29, 2011
  21. peteschulte

    peteschulte Private E-2

    Hey PC-XT,
    I will now try your change to line 31, but first, here's an update.

    I was longing for Visual Studio's ability to set breakpoints and debug. Now I have a way, thank you for introducing me to the JavaScript Error Console! I'll look for an introductory set of how-to's -- do you know of a good tutorial for the error console?

    The result in the console is
    The ":1" and the ":32" are right-justified, separate from the error message. Line 32 reads, " eval(text);" and you can see the whole anonymous function below.

    It is inside of getData(). Here it is

    Before seeking an answer to "Does eval() work on arrays?", where I figure that the author of the book knows what he's doing, let's try your replacement code.
    Thank you!
     
  22. peteschulte

    peteschulte Private E-2

    I understand/guess that we intend to replace every instance of "window.google.ac.Suggest_apply".

    Would you be willing to check the syntax of your substitute, please? I'm seeing 3 open parens and one close paren. I'll try reversing the 2nd to be closed, but don't yet know how to use reg ex in JS. Maybe that forward slash should go inside the first parens?

    Here is your new version followed by the previous version.

    As written, didn't work (no response when typing a letter into the search box). Cleared the browser cache, same result.

    Reversed the paren, cleared the browser cache, ran it, still no joy. More documentation to come.
     
  23. peteschulte

    peteschulte Private E-2

    Different Error Messages

    Note that for this test, I switched the 2nd paren
    I should go nail down how to use regular expressions.

    • typed "a" in the suggest box and captured the error msg.
    • typed "b" and captured the err msg.
    • screen-captured the code
    Changed back to the original code from the text book and observed a different error msg.

    Repeated the above three steps. Zipped and attached 6 screen caps. Before conjecturing, I'll see what you think.

    BTW, I did request this be moved to its own thread here. I just started a new topic and now wait for the Mods to read it. Maybe there's another way to deal with merging threads? It's not a big deal to me, but would make the forum content more accessible.
     

    Attached Files:

  24. PC-XT

    PC-XT Master Sergeant

    That's what I was expecting. Also, I just changed the line 31 code I gave earlier to make it faster and better:
    Code:
    var text = XMLHttpRequestObject.responseText.replace(/^\w*window\.google\.ac\..+\(/, "callback(");
    It could probably be defined more, but if it's changed that much, you might need other routines changed, anyway.

    As for the error console, since you are using chrome:
    http://code.google.com/chrome/devtools/docs/console.html
    http://www.tuttoaster.com/learning-javascript-and-dom-with-console/
    Some other suggestions are here:
    http://stackoverflow.com/questions/45965/how-do-i-use-the-javascript-console-in-google-chrome
     
  25. peteschulte

    peteschulte Private E-2

    Your Code and the Results

    XMLHttpRequestObject.onreadystatechange = function()
    {
    if (XMLHttpRequestObject.readyState == 4 &&
    XMLHttpRequestObject.status == 200) {
    var text = XMLHttpRequestObject.responseText.replace(/window\.google\.ac\..*\(/, "callback(");
    // the original code follows
    // var text = XMLHttpRequestObject.responseText.replace("window.google.ac.Suggest_apply", "callback");
    eval(text);
    }
    }

    Results click for details:
    Cannot read property 'length' of callback function

    Now we're gettin' somewhere. I learned a lot today.​
    Explain /window\.google\.ac\..*\(/,

    / I think this is the regex delimiter as in /==begin regex and /==end regex
    * Matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.

    \ Marks the next character as a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".
    \( matches ( see preceding about \
    . Matches any single character except "\n". To match any character including the '\n', use a pattern such as '[\s\S]. This means that .. matches .


    the rejected parts:

    window\.google\.ac\ not searched for in the regex syntax
    .. not found in the list at http://www.regextester.com/jssyntax.html
    *\ not found in the list at http://www.regextester.com/jssyntax.html
    (/ not found in the list at http://www.regextester.com/jssyntax.html
    ..*\ not found
    Thanks!

    So much for learning, let's get back to the results error msg.
     
    Last edited: Apr 29, 2011
  26. peteschulte

    peteschulte Private E-2

    as follows, gets the results below.

    XMLHttpRequestObject.onreadystatechange = function()
    {
    if (XMLHttpRequestObject.readyState == 4 &&
    XMLHttpRequestObject.status == 200) {
    // change #2
    XMLHttpRequestObject.responseText.replace(/^\w*window\.google\.ac\..+\(/, "callback(");
    // change #1 follows and I commented out its screen caps
    // var text = XMLHttpRequestObject.responseText.replace(/window\.google\.ac\..*\(/, "callback(");
    // the original code follows
    // var text = XMLHttpRequestObject.responseText.replace("window.google.ac.Suggest_apply", "callback");
    eval(text);
    }
    }

    Result: "onreadystatechange, text is not defined" from the attached screen caps.
    Seems that all the error messages point to "eval(text);"
    First was a TypeError: Cannot call method 'h' of undefined​
    Now what? Is the value returned too big?​

    Thanks for hanging in there with this one!
     

    Attached Files:

  27. peteschulte

    peteschulte Private E-2

  28. PC-XT

    PC-XT Master Sergeant

    Ok. Google did change the format. This new function h takes a single array, rather than the 4 parameters that callback function deals with. I changed the html page to this, which should work:
    Code:
    <html>
    <head>
    
    <title>Google Suggest</title>
    
    <style>
    #targetDiv {
    background-color: #FFAAAA;
    width: 40%;
    }
    </style>
    
    <script language = "javascript">
    function getData(dataSource)
    {
    var XMLHttpRequestObject = false;
    
    if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", dataSource);
    
    XMLHttpRequestObject.onreadystatechange = function()
    {
    if (XMLHttpRequestObject.readyState == 4 &&
    XMLHttpRequestObject.status == 200) {
    var text = XMLHttpRequestObject.responseText.replace("window.google.ac.h", "callback");
    eval(text);
    }
    }
    
    XMLHttpRequestObject.send(null);
    }
    }
    
    function askGoogleSuggest()
    {
    var input = document.getElementById("textField");
    
    if (input.value) {
    getData("google.php?qu=" + input.value);
    }
    else {
    var targetDiv = document.getElementById("targetDiv");
    
    targetDiv.innerHTML = "<div></div>";
    }
    }
    
    
    function callback(a){
    
    var searchTerm=a[0], arrayTerm=a[1];
    
    var data = "<table>";
    var loopIndex;
    
    for (loopIndex = 1; loopIndex < arrayTerm.length;
    loopIndex++) {
    data += "<tr><td>" +
    "<a href='http://www.google.com/search?q=" +
    arrayTerm[loopIndex][0] + "'>" + arrayTerm[loopIndex][0] +
    '</a></td><td>' + arrayTerm[loopIndex][2] + "</td></tr>";
    }
    
    data += "</table>";
    
    var targetDiv = document.getElementById("targetDiv");
    
    targetDiv.innerHTML = data;
    }
    </script>
    
    </head>
    
    <body>
    
    <H1>Handling Google Suggest</H1>
    
    Search for <input id = "textField" type = "text"
    name = "textField" onkeyup = "askGoogleSuggest()">
    
    <div id = "targetDiv"><div></div></div>
    
    </body>
    
    </html>
    (The php document doesn't need changing.)
    If Google ever changes the function again, this would stop working until you updated the function name from h to whatever the new one is and update the callback function to handle the new format. (The old name was Suggest_apply, and the old callback function had 2 big differences: It took parameters instead of a single array, and had the arrayTerm array as alternating key/value pairs instead of a regular ordered list of arrays with 3 items.) Since they've gone to using letters for the function name, they probably plan on changing it sometime in the future...
     
    Last edited: Apr 30, 2011
  29. peteschulte

    peteschulte Private E-2

    Congratulations and Thank You, PC-XT

    It works and I understand the before and after. I would like to know how you investigated and determined the Google change. All I did was to post a question here.

    Tuesday I have a level 2 job interview, so need to start familiarizing myself with Checkout by Amazon. I surely am glad to have this problem solved.

    Thanks again.
     
  30. PC-XT

    PC-XT Master Sergeant

    There should be one more change:
    In the callback function's for loop, loopIndex should start at 0, rather than 1. Starting at 1 skips the top result.


    As for the investigation:
    I placed the html you gave to view the results on the test site with the php. The errors did point to the eval, so I placed an alert just before it to show me what code was being run through it. I found no problems there, so I looked at the callback function. It was obvious, once I compared the code running through eval to the function, that it was throwing away the array as an unused parameter and expecting 3 more variables. From the variable names, I figured out how it was supposed to display, and modified the function to map the variables to parts of the array. I also had to modify the for loop to use the new arrayTerm format by removing the extra increment and adding array indices. The starting loopIndex variable also should be adjusted.

    I am glad to help. Thanks for the credit. ;)

    Good luck on your interview. :)
     
  31. peteschulte

    peteschulte Private E-2

    Hi PC-XT
    Both old and new versions are available here.

    Another tune-up came from the Google forum:

    This line enables my Google Suggest to handle a space between two words in the search term
    Thanks again for getting me through this and into using the console!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds