Hy,
I got switch case inside function when but when i call it, i got error Undefined Variable and i don't know why (i use PHP 8)
private function getIDFromBrand() {
switch ($this->brand) {
case "Niky":
$id = 1;
break;
case "Pumo":
$id = 4;
break;
case "Coke":
if ($this->typecoke== 0) {
$id = 2;
} else {
if ($this->typecoke== 1) {
$id = 3;
}
}
break;
case "Tomato":
$id = 5;
break;
case "Riles":
$id = 6;
break;
case "TEST":
$id = 7;
break;
}
return $id; // Error Undefined variable $id
}
When i declare $id at the top of my function, like
$id = null
or
$id = 0
The switch doesn't update it, so it will return null or 0, it will return the declared value.
Your switch statement has no default branch, so if $this->brand is, say, "Stack Overflow", it will not run any of the statements, and $id will never be set.
See the PHP manual for the switch statement:
A special case is the default case. This case matches anything that wasn't matched by the other cases.
Similarly, if $this->brand is "Coke", but $this->typecoke is, say, 42, it will not match either of the conditions in that branch.
switch ($this->brand) {
case "Niky":
$id = 1;
break;
case "Pumo":
$id = 4;
break;
case "Coke":
if ($this->typecoke== 0) {
$id = 2;
} elseif ($this->typecoke== 1) {
$id = 3;
} else {
$id = -1; // WAS PREVIOUSLY NOT SET
}
break;
case "Tomato":
$id = 5;
break;
case "Riles":
$id = 6;
break;
case "TEST":
$id = 7;
break;
default:
$id = -1; // WAS PREVIOUSLY NOT SET
break;
}
Related
I have checked the PHP source code and can not find the "require" function.
I only found a piece code relatives to it on main/main.c from line 976
switch (EG(current_execute_data)->opline->extended_value) {
case ZEND_EVAL:
function = "eval";
is_function = 1;
break;
case ZEND_INCLUDE:
function = "include";
is_function = 1;
break;
case ZEND_INCLUDE_ONCE:
function = "include_once";
is_function = 1;
break;
case ZEND_REQUIRE:
function = "require";
is_function = 1;
break;
case ZEND_REQUIRE_ONCE:
function = "require_once";
is_function = 1;
break;
default:
function = "Unknown";
}
Do you know which file defines the "function"?
I have this method:
private function convertStatusStringToIntZeroOrOne(string $status)
{
$status = strtolower($status);
switch ($status) {
case "off":
case "0":
case 0:
$int_status = 0;
break;
case "on":
case "1":
case 1:
$int_status = 1;
break;
default:
$int_status = 1;
break;
}
return $int_status;
}
The $status parameter, when is the string "On" (with O letter capitalize), return 0 (zero).
Of course, I need return as 1.
Thank you
As you had numeric 0 and 1 in the options of the switch it was using a numeric comparison - "on" to a number is 0 and so it matched against 0.
As you have the parameter as type string a number would be converted to a string, so remove the numeric comparisons...
function convertStatusStringToIntZeroOrOne(string $status)
{
$status = strtolower($status);
switch ($status) {
case "off":
case "0":
$int_status = 0;
break;
case "on":
case "1":
$int_status = 1;
break;
default:
$int_status = 1;
break;
}
return $int_status;
}
echo convertStatusStringToIntZeroOrOne("On");
Although you could reduce the function to...
function convertStatusStringToIntZeroOrOne(string $status)
{
$status = strtolower($status);
return ($status == "off" || $status == 0)?0:1;
}
if (isset($_POST["submit"])){
$oride='';
$count = "25";
$origin = $_POST["origin"];
$destinataion = $_POST["destination"];
$oride = ($destination = $_POST["destination"] - $origin= $_POST["origin"]);
switch (true) {
case ($count<="0"):
echo "invalid";
break;
case ($count==="15"):
echo $count;
break;
case ($count==="16"):
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
} }
The code will compute 1st then execute switch depending on what is the result of the computation. I tried if else but it will be too long because the case will go up to 130.
You must use the var $count in switch statement and the constant in case this way
switch ($count) {
case "0" :
echo "invalid";
break;
case "15":
echo $count;
break;
case "16":
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
break;
}
You have to provide an expression to the switch statement, while the case statements are just "versions" of the result of that expression. The only thing you can NOT do directly is the "<= 0" expression, but you can work around it:
if (isset($_POST["submit"])){
$oride='';
$count = "25";
$origin = $_POST["origin"];
$destinataion = $_POST["destination"];
$oride = ($destination = $_POST["destination"] - $origin= $_POST["origin"]);
// --- normalize $count:
$count = $count <= 0 ? 0 : $count;
// use $count as expression:
switch ($count) {
case 0:
echo "invalid";
break;
case "15":
echo $count;
break;
case "16":
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
} }
The problem I seem to be having is that anytime I reference the $j variable it switches which makes sense since it literally is assigned to a function that does that. I may be just having a programmers block but, how do I make it so it generates a random number one time and I can assign it to a variable that won't change?
Thanks in advance!
global $wpdb;
$j = rand();
$table_name = 'quiz';
if(isset($_POST["nextpage"]) && $_POST["nextpage"]!="") {
$j = rand();
$nextpage = mysql_real_escape_string($_POST["nextpage"]);
switch ($nextpage) {
case '2':
$wpdb->insert($table_name, array('Q1score'=>$_POST[Q1],'Q2score'=>$_POST[Q2],'Q3score'=>$_POST[Q3],'ID'=>$j));
$_SESSION['page'] = 2;
break;
case '3':
$wpdb->update($table_name, array('Q4score'=>$_POST[Q4],'Q5score'=>$_POST[Q5],'Q6score'=>$_POST[Q6],'Q7score'=>$_POST[Q7]), array('ID'=>$j));
$_SESSION['page'] = 3;
break;
case '4':
$_SESSION['page'] = 4;
break;
}
} else {
if (!isset($_SESSION["page"])) {
$_SESSION["page"] = "1";
} else {
/* do nothing */
}
}
Use isset() to see if it's already been defined in this scope.
if ( !isset( $j ) ) $j = rand();
Populate a $_SESSION[] variable if you want to maintain across multiple page loads during one session.
if ( !isset( $_SESSION['j'] ) ) $_SESSION['j'] = rand(); // expects session_start() to be called somewhere above
$j = $_SESSION['j'];
Pick one or the other.
global $wpdb;
if ( !isset( $_SESSION['j'] ) ) $_SESSION['j'] = rand(); // expects session_start() to be called somewhere above
$j = $_SESSION['j'];
$table_name = 'quiz';
if(isset($_POST["nextpage"]) && $_POST["nextpage"]!="") {
/* $j = rand(); remove this assignment */
$nextpage = mysql_real_escape_string($_POST["nextpage"]);
switch ($nextpage) {
case '2':
$wpdb->insert($table_name, array('Q1score'=>$_POST[Q1],'Q2score'=>$_POST[Q2],'Q3score'=>$_POST[Q3],'ID'=>$j));
$_SESSION['page'] = 2;
break;
case '3':
$wpdb->update($table_name, array('Q4score'=>$_POST[Q4],'Q5score'=>$_POST[Q5],'Q6score'=>$_POST[Q6],'Q7score'=>$_POST[Q7]), array('ID'=>$j));
$_SESSION['page'] = 3;
break;
case '4':
$_SESSION['page'] = 4;
break;
}
} else {
if (!isset($_SESSION["page"])) {
$_SESSION["page"] = "1";
} else {
/* do nothing */
}
}
I am having a small problem with my PHP MySQL Select. The function is inside of a PHP class. Here is the error I get:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
integer given in C:\xampp\htdocs\include\database.php on line 59
Warning: extract() expects parameter 1 to be array, null given in
C:\xampp\htdocs\include\database.php on line 59
The function just simply updates the database to show what browser and OS they visited the site with. The function is called from another file that is called by an AJAX call that uses POST to send the data about the OS and browser that was gathered from a Javascript file. It only fails if there is an entry of the IP address already in the database. If there is no IP Address entry in the database it succeeds in creating one.
Here is my code:
function addStat($browser, $os){
$IE = 0; $Firefox = 0; $Safari = 0; $Opera = 0; $Chrome = 0; $otherb = 0;
$Windows = 0; $Linux = 0; $Mac = 0; $Android = 0; $iOS = 0; $otheros = 0;
$ql = 0; $totalVisits = 0;
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$q1 = mysql_query("SELECT * FROM " . DB_STATS . " WHERE ip='$ip'", $this->connection);
if (mysql_num_rows($q1)==0){
$browser = mysql_real_escape_string($browser);
$os = mysql_real_escape_string($os);
switch($browser){
case "Internet Explorer":
$IE += 1;
break;
case "Firefox":
$Firefox += 1;
break;
case "Safari":
$Safari += 1;
break;
case "Opera":
$Opera += 1;
break;
case "Chrome":
$Chrome += 1;
break;
default:
$otherb += 1;
break;
}
switch($os){
case "Windows":
$Windows += 1;
break;
case "Mac OS X":
$Mac += 1;
break;
case "Linux":
$Linux += 1;
break;
case "Android":
$Android += 1;
break;
case "iOS":
$iOS += 1;
break;
default:
$otheros += 1;
break;
}
$q = $this->query("INSERT INTO " . DB_STATS . " VALUES (null, '$ip', '$Chrome', '$IE', '$Firefox', '$Opera', '$Safari', '$otherb', '$Windows', '$Mac', '$Linux', '$Android' , '$iOS' , '$otheros', 1)");
if ($q == true){
return(1);
}
else{
return(0);
}
}
else if (mysql_num_rows($q1)==1){
extract(mysql_fetch_array($ql));
switch($browser){
case "Internet Explorer":
$IE += 1;
break;
case "Firefox":
$Firefox += 1;
break;
case "Safari":
$Safari += 1;
break;
case "Opera":
$Opera += 1;
break;
case "Chrome":
$Chrome += 1;
break;
default:
$otherb += 1;
break;
}
switch($os){
case "Windows":
$Windows += 1;
break;
case "Mac OS X":
$Mac += 1;
break;
case "Linux":
$Linux += 1;
break;
case "Android":
$Android += 1;
break;
case "iOS":
$iOS += 1;
break;
default:
$otheros += 1;
break;
}
$totalVisits += 1;
$q = $this->query("UPDATE " . DB_STATS . " set Chrome='$Chrome', IE='$IE', Firefox='$Firefox', Opera='$Opera', Safari='$Safari', otherb='$otherb', Windows='$Windows', Mac='$Mac', Linux='$Linux', Android='$Android' , iOS='$iOS' , otheros='$otheros', totalVisits='$totalVisits'");
if ($q == true){
return(1);
}
else{
return(0);
}
}
else{
return(-1);
}
}
I hope everything made sense and that someone will help.
I see it now -- you used $ql (lower case L) when you intend to use $q1. Let this be a lesson against using very short variable names or very similar names.
// $ql was initialized to 0
$ql = 0; $totalVisits = 0;
// $q1 holds the result resource
extract(mysql_fetch_array($q1));
It is not advisable to call extract() on the output of mysql_fetch_array() unless you also specify the second parameter MYSQL_ASSOC as the fetch type. By default it returns both numeric and associative indices for each column.
extract(mysql_fetch_array($q1, MYSQL_ASSOC));
// Or better
extract(mysql_fetch_assoc($q1));
In general, I would probably advise against using extract() in most any situation, since it results in numerous variables dumped into the global namespace, in particular when you have done SELECT * without being specific about which columns are selected. Better to access them via their array:
$row = mysql_fetch_assoc($q1);
echo $row['browser'];