echo 76 <=> '76 trombones';
Both sides of the “spaceship” are equal, so the answer is 0. PHP will convert ‘76 trombones’ to 76 in this context, as the string starts with ‘76’. Try it! For php 8.0 and forward the answer is [x] -1, for previous versions the answer is [x] 0. PHP 8 changed the way non-strict comparison between numbers and non-numeric strings work.
$encrypted = shal($password);$encrypted = crypt($password, \$salt);$encrypted = md5($password);$encrypted = password_hash($password, PASSWORD_DEFAULT);$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($email === false) {
$emailErr = "Please re-enter valid email";
}
1 <?php
2 $count = 0;
3 $_xval = 5;
4 $_yval = 1.0;
5 $some_string = "Hello there!";
6 $some_string = "How are you?";
7 $will i work = 6;
8 $3blindmice = 3;
9 ?>
| [ ] |
$string_name = "testcookie";
$string_value = "This is a test cookie";
$expiry_info = info()+259200;
$string_domain = "localhost.localdomain";
$_REQUEST is missing.$_COOKIES array is missing.setcookie() is missing.$total = 2 + 5 * 20 - 6 / 3
$dog = new Pet;$horse = (new Pet);$cat = new Pet();if (!$_SESSION['myusername'])
{
header('locaton: /login.php');
exit;
}
/* This is a comment */ignore_user_abort( ) function sets whether a client disconnect should abort a script execution. In what scenario would you, as a web developer, use this function? <?php
echo array_reduce([1, 2, 5, 10, 11], function ($item, $carry) {
$carry = $carry + $item;
});
?>
<?php
echo array_reduce([1, 2, 5, 10, 11], function ($carry, $item) {
return $carry = $item + $item;
});
?>
<?php
array_reduce([11 2, 5, 10, 11], function ($item, $carry) {
echo $carry + $item;
});
?>
<?php
echo array_reduce([1, 2, 5, 10, 11], function ($carry, $item) {
return $carry += $item;
});
?>
class MyClass {
public function _construct()
{
echo 'Winter is almost over!'."\n";
}
}
$userclass = new MyClass;
class MyClass {
public function _construct()
{
echo 'Winter is almost over!.."\n";
}
}
$userclass = new MyClass;
class MyClass {
public function _construct()
{
echo 'Winter is almost over!.."\n";
}
}
$userclass = new MyClass;
class MyClass {
public function _construct()
{
echo 'Winter is almost over!'."n";
}
}
$userclass = MyClass;
/* Space: the final frontier */*/ Space: the final frontier /*#Space: the final frontier// Space: the final frontier<?php echo "How much are the bananas?"?>How much are the bananas?function process(...$vals) {
// do some processing
}
Horse class exists, which is a valid example of inheritance in PHP?class Pegasus extends Horse {}class Alicorn imports Pegasus, Unicorn {}class Unicorn implements Horse {}class Horse inherits Unicorn {}ini_set('display_errors',1);seasons=array(
1=>'spring',
2=>'summer',
3=>'autumn',
4=>'winter',
);
$seasons=array(spring,summer,autumn,winter);$seasons=('spring','summer','autumn','winter');$seasons=['spring','summer','autumn','winter'];self and this are keywords that can be used to refer to member variables of an enclosing class. The difference is that $this->member should be used for members and self::$member should be used for members$mathe=array('archi','euler','pythagoras');
array_push($mathe,'hypatia');
array_push($mathe,'fibonacci');
array_pop($mathe);
echo array_pop($mathe);
echo sizeof($mathe);
isset ($_GET['fav_band'])
fav_band is included in the query string at the top of your browserprint_r($_REQUEST);$cupcakes?print_r($cupcakes);var_dump($cupcakes);foreach($cupcakes as &$cupcake) echo $cupcake;header() command that you are using for a redirectelsebreakreturncontinuephp -hphp infophp -vphp -mif (!empty($_POST["mail"])) {
echo "Yes, mail is set";
} else {
echo "No, mail is not set";
} (correct)
$result in this calculation?$result = 25 % 6;
$string = 'Shylock in a Shakespeare's "Merchant of Venice" demands his pound of flesh.';
$db has been set up to use for database operations, including user authentication. All user-related properties are set. The script line public function __construct(&$db) shows a constructor that initializes all user-related properties to _ if no user has logged in. These parameters will be properly set by the login functions when a user logs in$first_name and $family_name are valid strings, which statement is invalid?echo $first_name. ' '. $family_name;print $first_name, ' ', $family_name;print $first_name. ' '. $family_name;echo $first_name, ' ', $family_name; class Cow extends Animal {
private $milk;
}
class Cow {
public $milk;
}
$daisy = new Cow();
$daisy->milk = "creamy";
class Cow {
public $milk;
function getMilk() {`
return $this->milk;
}
}
class Cow {
private $milk;
public function getMilk() {
return $this->milk;
}
}
<books>
<book>
<title>A Tale of Two Cities</title>
<author>Charles Dickens</author>
<categories>
<category>Classics</category>
<category>Historical</category>
</categories>
</book>
<book>
<title>Then There Were None</title>
<author>Agatha Christies</author>
<categories>
<category>Mystery</category>
</categories>
</book>
</books>
$books = simplexml_load_string('books.xml');
echo $books->book[0]->categories->category[1];
$books = simplexml_load_file('books.xml');
echo $books->book[0]->categories->category[1];
$books = SimpleXMLElement('books.xml');
echo $books->book[0]->categories->category[1];
$books = SimpleXML('books.xml');
echo $books->book[0]->categories->category[1];
function doStuff($haystack, $needle) {
$length = strlen($needle)
if (substr($haystack, 0, $length) == $needle)
return true;
else
return false;
}
equalsendsWithstartsWithcontainsisset($_POST['submit'])
print_r($_SESSION); report_errors = E_ALL
display_errors = On
error_reporting = E_ALL
display_errors = On
error_reporting = E_ALL & ~E_NOTICE
display_errors = Off
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
$Double$double$_2times$2times$string = "https://cat-bounce.com";?sub($string, -3)substr($string, -3)substr($string, 3)$string.substr(-3)__RESOURCE____FUNCTION____CLASS____TRAIT__ if( 1 == true){
echo "1";
}
if( 1 === true){
echo "2";
}
if("php" == true){
echo "3";
}
if("php" === false){
echo "4";
}
$secret_word = 'if i ate spinach';
setcookie('login', $_REQUEST['username']. ','. md5($_REQUEST['username'].$secret_word));
$var is a variable then $$var is a variable variable whose name is the value of $var. Which script produces the output below, using variable variables?Cat
Dog
Dog
$name = "Cat";
$name = "Dog";
echo $name . "<br/>";
echo $$name . "<br/>";
echo $Dog;
$name = "Cat";
$$name = "Dog";
echo $name . "<br/>";
echo $$name . "<br/>";
echo $Dog;
$name = "Cat";
$$name = "Dog";
echo $name . "<br/>";
echo $$name . "<br/>";
echo $Cat;
$name = "Cat";
$$name = "Dog";
echo $name . "<br/>";
echo $name . "<br/>";
echo $Cat;
<?php
start_session();
$music = $_SESSION['music'];
?>
<?php
session_start();
$music = $SESSION['music'];
?>
<?php
start_session();
$music =$session['music'];
?>
<?php
session_start();
$music = $_SESSION['music'];
?>
<?php
$dates = array('2018-02-01', '2017-02-02', '2015-02-03');
echo "Latest Date: ". max($dates)."\n";
echo "Earliest Date: ". min($dates)."\n";
?>
<?php
$dates = array('2018-02-01', '2017-02-02', '2015-02-03');
echo "Latest Date: ". min($dates)."\n";
echo "Earliest Date: ". max($dates)."\n";
?>
<?php
$dates = array('2018-02-01', '2017-02-02', '2015-02-03');
echo "Latest Date: ". ($dates)."\n";
echo "Earliest Date: ". ($dates)."\n";
?>
<?php
$dates = array('2018-02-01', '2017-02-02', '2015-02-03');
echo "Latest Date: " max($dates)."\n";
echo "Earliest Date: " min($dates)."\n";
?>
$kilometers = 1;
for (;;) {
if ($kilometers > 5) break;
echo "$kilometers kilometers = ".$kilometers*0.62140. " miles. <br />";
$kilometers++;
}
kilometers = 0.6214 miles.
kilometers = 1.2428 miles.
kilometers = 1.8642 miles.
kilometers = 2.4856 miles.
kilometers = 3.107 miles.
kilometers = 0.6214 miles.
kilometers = 1.2428 miles.
kilometers = 1.8642 miles
kilometers = 2.4856 miles.
kilometers = 3.107 miles.
kilometers = 3.7284 miles.
kilometers = 1.2428 miles.
kilometers = 1.8642 miles.
kilometers = 2.4856 miles.
kilometers = 3.107 miles.
$_SERVER$SERVER_VARIABLES$_ENV$GLOBALS$capitals = ['UK' => 'London', 'France' => 'Paris'];
echo "$capitals['france'] is the capital of France.";
Also, ‘france’ key must be capitalized!
$HTTP_SERVER_VARS("REMOTE_IP")$_SESSION["REMOTE_ADDR"];$_SERVER["HTTP_X_FORWARDED_FOR"]getenv("REMOTE_ADDR")Both 2 and 4 are correct!
upload_max_filesize configuration parameter.$my_text = 'The quick grey [squirrel].';
preg_match('#\[(.*?)\]#', $my_text, $match);
print $match[1]."\n";
$fruits = ['apple', 'orange', 'pear', 'mango', 'papaya'];
$i = 0;
echo $fruits[$i+=3];
<!-- include file="gravy.php"; --><?php include gravy.php; ?><?php include "gravy.php"; ?><?php include file="gravy.php"; ?>session_start() and filter_input()filter_var() and filter_input()preg_match() and strstr()$statement->bindValue(':name', '%' . $_GET['name'] . '%');$statement->bindValue('%' . $_GET['name'] . '%', ':name');$statement->bindParam(':name', '%' . $_GET['name'] . '%');$statement->bindParam('%' . $_GET['name'] . '%', ':name');$array1 as the keys and $array2 as the values$array1 = ['country', 'capital', 'language'];
$array2 = ['France', 'Paris', 'French'];
$array3 = array_merge($array1, $array2);$array3 = array_union($array1, $array2);$array3 = array_keys($array1, $array2);$array3 = array_combine($array1, $array2);$r is 255, and $g and $b are both 0. What is the correct code to output "#ff0000"?printf('#%2x%2x%2x', 255, 0, 0);printf('#%2X%2X%2X', $r, 0, 0);printf('#%x%x%x', 255, 0, 0);printf('#%02x%02x%02x', 255, 0, 0);$xmas = new DateTime('Dec 25, 2018');
$twelfth_night = $xmas->add(new DateInterval('P12D'));
echo $twelfth_night->format('l');$twelfth_night = strtotime('December 25, 2018 + 12 days');
echo date('d', $twelfth_night);
$twelfth_night = strtotime('December 25, 2018 + 12 days');
echo strftime('%d', $twelfth_night);
$xmas = new DateTime('Dec 25, 2018');
$twelfth_night = $xmas->add(strtotime('12 days'));
echo $twelfth_night->format('D');1 seems correct, but the question asks for “day”, not day of the week. Twelfth Night is the “06” day of January, 2019.
$i = 1;
while ($i < 10) {
echo $i++ . '<br/>';
}
$i = 0;
while ($i <= 10) {
echo $i++ . '<br/>';
}
while ($i <= 10) {
echo ++$i . '<br/>';
}
$i = 0;
while ($i < 10) {
echo ++$i . '<br/>';
}
break, continue, do-while, exception, for, foreach, if, switch, throw, whilevalues, operators, expressions, keywords, commentsfor, foreach, if, else, else if, switch, tries, throws, whileif-then-else, do-while, for-each, go-to, stop-whendiereturnthrowbreak$numbers = array(4,6,2,22,11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++){
echo $numbers[$x];
echo "<br />";
}
toString() in PHP?if( isset($user_info['url']) ) {
$_SESSION["loggedIn"] = true;
$_SESSION["username"] = $myusername;
header('Location: ' . $user_info['url']); //Redirects to the supplied url from the DB
} else {
header("Location: error.htm");
}
echo 5 % 0.75;
!empty($_GET['test'])isset($_GET['test'])$_GET['test'] == ''Actually both are correct, option 3 is actually testing if a checkbox is not set
if(empty($_POST['email'])) {
echo "The email cannot be empty";
}
if(empty($_GET['email'])) {
echo "The email cannot be empty";
}
if(empty($_POST('email'))) {
echo "The email cannot be empty";
}
if(isset($email)) {
echo "The email cannot be empty";
}
$valid = ip2long($ip) !== false;
$ip_address = "164.12.2540.1";
if(filter_var($ip_address, FILTER_VALIDATE_IP)){
echo "$ip_address is a valid IP address";
} else {
echo "$ip_address is not a valid IP address";
}
$ip_address = "164.12.2540.1";
if(validate_ip($ip_address)){
echo "$ip_address is a valid IP address";
} else {
echo "$ip_address is not a valid IP address";
}
$ip_address = "164.12.2540.1"
echo is_valid($ip_address, VALIDATE_IP);
$i = 0;
while($i < 6) {
if($i++ == 3) break;
}
echo "loop stopped at $i by break statement";
$dof->setTitle("Spot");
$cat->setTitle("Mimi");
$horse-?setTitle("Trigger");
$dog->setPrice(10);
$cat->setPrice(15);
$horse->setPrice(7);
print_r($cat);
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
while ($fruit_name = current($array)) {
if ($fruitname == 'apple') {
echo key($array).'<br />';
}
next($array);
}
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple')
echo key($array).'<br />';
}
next($array);
}
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
class Smurf {
public $name = "Papa Smurf";
public function __construct($name) {
$this->name = $name;
}
public function set_name($name) {
$name = $name;
}
}
$smurf = new Smurf("Smurfette");
$smurf->set_name("Handy Smurf");
echo $smurf->name;
if ($_FILES['image']['error'] === 0) {
move_uploaded_file($_FILES)['image']['temp_name'],
$path . $_FILES['image']['name']);
)
if ($_FILES['image']['error'] === false) {
move_uploaded_file($_FILES)['image']['temp_name'],
$path . $_FILES['image']['name']);
)
if ($_FILES['image']['error'] == 0) {
copy($_FILES)['image']['temp_name'],
$path . $_FILES['image']['name']);
)
if ($_FILES['image']['error'] == false) {
upload_file($_FILES)['image']['temp_name'],
$path . $_FILES['image']['name']);
)
$_GET$GLOBALS$_SESSION$_SERVER<?
for ($i=1; $i <= 10; $i++) {
echo $i;
}
?>
<?
$i = 10;
while($i>=0) {
echo $i;
$i--;
}
?>
<?
for($i = 10; $i > 0; $i++) {
print "$i <br />\n";
} // end for loop '''
?>
<?
for($i = 10; $i > 0; $i--) {
print "$i <br />\n";
} // end for loop
?>
function knights(){
return "a shrubbery";
}
if (knights())
printf "you are just and fair";
else
printf "NI!";
Our country is United States of America Our country has a total of 50 states
define('country',"United States of America");
define('states',50);
echo "Our country is "country"<br>";
echo "Our country has a total of ".states." states";
define('country',"United States of America");
define('states',50);
echo "Our country is ".country."<br>";
echo "Our country has a total of ".states." states";
define(country,"United States of America");
define('states',50);
echo "Our country is ".country."<br>";
echo "Our country has a total of ".states." states";
define('country',"United States of America");
define('states','fifty');
$K = 'strval'; echo "Our {$K(Country)} has {$K(FIFTY)} states.";
try{
echo "bodacious";
throw new Exception();
} catch (Exception $e) {
echo "egregious";
} finally {
echo "excellent";
}
1 function append($initial){ return $newString = $initial . ‘ belong to us’; } 2 $initialString = ‘All your base are’; 3 $initialString = append($initialString); 4 echo $initialString;
1 function append(&$initial){ return $newString = $initial . ‘ belong to us’; } 2 $initialString = ‘All your base are’; 3 append($initialString); 4 echo $initialString;
1 function append(&$initial){ return $newString = $initial . ‘ belong to us’; } 2 $initialString = ‘All your base are’; 3 $initialString = append($initialString); 4 echo $initialString;
1 function append(&$initial){ $initial = $initial . ‘ belong to us’; } 2 $initialString = ‘All your base are’; 3 append($initialString); 4 echo $initialString;
$believable = 'false';
$myth = 'The moon is made of green cheese';
$calc = 10**3+1;
if ($believable) {
echo $myth;
}
else {
echo $calc;
}
Explanation : ‘false’ evaluates to true since it is a string so the if condition is met.
return
else
break
continue
$x = "5";
$y = &$x;
$y = "2$y";
echo $x;
$array = array(1, 2, 3, 4, 5);
echo array_sum($array) / count($array);
$str = "Hello";
$str[0] = "J";
echo $str;
$a = array(1, 2, 3);
$b = array("one", "two", "three");
$c = array_combine($a, $b);
print_r($c);
$str = "abcdef";
echo substr($str, -2, 1);
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y;
$a = "Hello";
$b = &$a;
$b = "World";
echo $a;