Merkaber Posted October 6, 2015 Share Posted October 6, 2015 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 comment Share on other sites More sharing options...
justsomeguy Posted October 6, 2015 Share Posted October 6, 2015 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 comment Share on other sites More sharing options...
Merkaber Posted October 6, 2015 Author Share Posted October 6, 2015 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 comment Share on other sites More sharing options...
Ingolme Posted October 6, 2015 Share Posted October 6, 2015 $hint gets a value because it's in a loop. Link to comment Share on other sites More sharing options...
davej Posted October 6, 2015 Share Posted October 6, 2015 <?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 comment Share on other sites More sharing options...
justsomeguy Posted October 6, 2015 Share Posted October 6, 2015 This is the part that changes the value of $hint: foreach($a as $name) { if (stristr($q, substr($name, 0, $len))) { if ($hint === "") { $hint = $name; } else { $hint .= ", $name"; } } } Link to comment Share on other sites More sharing options...
Merkaber Posted October 6, 2015 Author Share Posted October 6, 2015 Okay guys, sorry for adding this thread... I am so blind -.- . Thank you all for your replys now I got it *head on table* Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now