Jump to content

PHP - AJAX example question

Recommended Posts

Hello everyone,


I have a question about the PHP - AJAX example (http://www.w3schools.com/php/php_ajax_php.asp).

In the PHP file, there is the variable " $hint ". But what is this variable used for?

Here the code:

$q = $_REQUEST["q"];$hint = "";// lookup all hints from array if $q is different from ""if ($q !== "") {    $q = strtolower($q);    $len=strlen($q);    foreach($a as $name) {        if (stristr($q, substr($name, 0, $len))) {            if ($hint === "") {                $hint = $name;            } else {                $hint .= ", $name";            }        }    }}

You are declaring $hint = "". After there is no input, that could happen to $hint. Why there is the if clause?

Maybe I am blind, but I don't get it.


Second questions is about this code part:

echo $hint === "" ? "no suggestion" : $hint;

How you can use operators without function, what does ' $hint === "" ' and what does ' : ' ?I already red the whole example more than twice... but I don't get it.

A short explanation or a link to another website which is explaining these would be awesome. Thank you in advance!


(By copy and pasting, the example works, but I wanna understand it)(I am not native English speaker, please excuse my mistakes)

Link to post
Share on other sites

The first if statement is just checking if the input is empty. If the input is empty then it doesn't do anything else.This line:

echo $hint === "" ? "no suggestion" : $hint;
is the same as this:
if ($hint === "") {  echo "no suggestion";}else {  echo $hint;}
The "?:" operator is just a shorthand operator for that kind of if statement.
Link to post
Share on other sites

Thank you for your reply,

I understand the if clause, but I don't understand, why we need this if clause. There is no way, that $hint is not empty. $hint is for all times empty, thats what I understand. Because we declare it empty at the top and there is no other input for $hint.

Link to post
Share on other sites
<?php// Array with names$a[] = "Anna";$a[] = "Brittany";$a[] = "Cinderella";// get the q parameter from URL$q = $_REQUEST["q"];$hint = "";// lookup all hints from array if $q is different from ""if ($q !== "") {    $q = strtolower($q); // change input to lower-case    $len = strlen($q); // get length of input string    $asize = sizeof($a); // get size of array    for($i=0; $i<$asize ; $i++) { // for each location in the array        $name = $a[$i];        if (stristr($q, substr($name, 0, $len))) { //if array characters match input string            if ($hint === "") {                $hint = $name; // set $hint to name in array            } else {                $hint .= ", $name"; // append name in array to $hint            }        }    }}if ($hint === "") { // if nothing matching was found in the array  echo "no suggestion";}else{  echo $hint; // echo the hint or hints}?> 
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...