why does my function always return false? - php

why does my function always return false?
i think the problem is caused by the isset function but i really dont know how to fix it
$big = array(
2,3,5,7,11,13,17,19,23
,29,31,37);
$fbig = array_flip ($big);
function isprime($n){
if($n < 2){
return FALSE;
}
if($n > 2147483647){
return FALSE;
}
if($n < 46341){
if(isset($fbig[$n])){
return TRUE;
} else {
return FALSE;
}
}
}
$b = 11;
if(isprime($b)){echo "lol";}

if(isset($fbig[$n])){
This line is the problem.
What you want to check is not isset($fbig[$n]) (which checks if there is something in the array at the index $n) but in_array($n, $fbig) (which checks if the array $fbig contains the value $n).
The array $fbig is not in the scope of the function since it's defined outside. But you can pass it:
if(isprime($b, $fbig)){echo "lol";}
should work just fine.

because your looking for a key, not a value
$fbig[11] is not set
you'll want to use in_array()
in this case, there are 11 items, but they are numbered from 0-10, no 11
plus, like Sarfraz said, it needs to be global

It's because your function doesn't know what $fbig is. A quick fix would be to change your function to look like this:
function isprime($n){
global $fbig;
if($n < 2){
return FALSE;
}
if($n > 2147483647){
return FALSE;
}
if($n < 46341){
return isset($fbig[$n]); // Nit picking fix!
}
}

Related

While loop on multi-dimensional array

So I have a function that currently has a foreach and it works amazing, but I'm being forced to change it to a while loop:
PLEASE NOTE: The developers at my company don't want to use the foreach and they think that a while loop would be more efficient, but I'm not understanding how that would be executed, so I need some help.
So I have the following function ($post_blocks is an array of arrays):
public function parse_block_data(string $block_name, string $selector, $post_id)
{
if (!has_blocks($post_id)) {
return false;
}
$post_blocks = parse_blocks(get_the_content('', false, $post_id));
foreach ($post_blocks as $block) {
if ($block_name != $block['blockName']) {
continue;
}
if (!isset($block['attrs']['id'])) {
return false;
}
if (isset($block['attrs']['data'][$selector])) {
return $block['attrs']['data'][$selector];
} else {
break;
}
}
return false;
}
It uses the parameters to build up an array as shown below:
Output
So I started building a while loop inside the function, but I'm clueless on how to achieve it without using a foreach or if it's even possible, so I replaced the foreach with:
// I get the 9 counts of $post_blocks correctly.
$block = 0;
while ($block < count($post_blocks))
// If the $block_name doesn't match `blockName` value inside the multi-dimensional array, then continue iterating until the end and then return false.
// If ['attrs']['id'] is not set, return false.
// At last, if we have a blockName and a ID and the selector is set, return ['attrs']['data'][$selector]
}
All help will be appreciated! It makes no sense to me, but if someone can assist, I'd be forever grateful!
It's basically the same as your foreach loop, you just set the iteration variable by indexing the array, and increment the index manually.
$block_num = 0;
while ($block_num < count($post_blocks)) {
$block = $post_blocks[$block_num];
if ($block_name == $block['blockName']) {
if (!isset($block['attrs']['id'])) {
return false;
}
if (isset($block['attrs']['data'][$selector])) {
return $block['attrs']['data'][$selector];
} else {
break;
}
}
$block_num++;
}
I'm not sure why your colleagues think this is preferable.
If there's a company coding style they want you to follow, why don't you ask them what it should be?

Set function value based on variable

I need to assign the variable Freight_class to the value: Bigheavy, Largebox or Littlebox. For that I have to use the value of Freight. If the value of Freight is greater than 225, Freight_class must be set to Bigheavy. If Freight is greater than 99, Freight_class must be equal to Largebox. Under 99, Freight_class must be Littlebox.
Say Freight=40, then Freight_class should be Littlebox. But I can not get it to work. What do I do wrong
<?php
$Freight=40;
function Freight_class($Freight) {
if ($Freight > '225') {
return ('Bigheavy');
} elseif ($Freight > '99') {
return ('Largebox');
} else {
return ('Littlebox');
}
}
echo $Freight_class;
You never actually call your function. And $Freight_class is not defined anywhere.
<?php
$Freight=40;
function Freight_class($Freight) {
if ($Freight > 225) {
return ('Bigheavy');
} elseif ($Freight > 99) {
return ('Largebox');
} else {
return ('Littlebox');
}
}
$Freight_class = Freight_class($Freight); // assign the result of your function to $Freight_class
echo $Freight_class;
<?php
$Freight=40;
function Freight_class($Freight) {
if ($Freight > 225) {
return ('Bigheavy');
} elseif ($Freight > 99) {
return ('Largebox');
} else {
return ('Littlebox');
}
}
echo Freight_class($Freight);
This way it would work. You have some logical errors in your code. First, it seems like youre confused with the terms class and function it looks like you tought to create a class with your function Freight_class but later your using it like a normal variable.
Next point is that you compare strings with integers which could lead to strange bugs.
Working code with comments:
<?php
$Freight=40;
// the fact that the parameter has the same hame as the variable
// does not mean that it is using the variable. The new $Freight
// is block scoped and prevents access of the outer $Freight
function Freight_class($Freight) {
// while comparing number you should use numbers and not a string
// containing a number
if ($Freight > 225) {
return 'Bigheavy';
} elseif ($Freight > 99) {
return 'Largebox';
}
return ('Littlebox');
}
// since Freight_class is a function and the inner $Freight differs
// from the outer one, you have to call Freight_class with the outer
// $Freight as parameter
echo Freight_class($Freight);

in a function can you retun a null to exit the function PHP

I am passing the two var's by ref to change them, and once I have changed them or one of the (8 loops) have found a positive in the string I am using, I want to exit the function, but I don't need to return anything because they are passed by ref.
I could just pass a copy of one and then ref the other one and set the var of the one that is copied = to the function and return that, but is there a cleaner way where I just call the function, the vars are set and I can move on?
function get_cat_size($urlstr, &$cat, &$size){ return null; };
$cat = get_cat_size($urlstr, &$size);
Does the first one work or not? Witch is better for readability?
Thanks for the input!
while( $i < $countz )
{
$pos = strpos($outdoor, $outdoor[$i]);
if($pos != false)
{
$cat = $outdoorID;
while( $j < $sizeArrayCount)
{
$poz = strpos($outdoor, $outdoor[$i]);
if($poz != false)
{
$size = $outdoorID;
return;
}
$j++;
}
return;
}
$i++;
}
^ so this should work yes no maybe so?
So this is one of 8 loops set up in a order because they are least important to important, with different var = different stores.
You can just return without a value:
function returnsNothing (&$a, &$b) {
return;
}
Or simpler just omit the return statement at all
function returnsNothing (&$a, &$b) {
// do something
}
Both snippets will make the function returning NULL.
Take a look at break for returning out of your for loops.
I would personally avoid returning null within a function since NULL will be returned by default when there is no return value specified. You can read more here at PHP: Returning values

How to write a function that checks if a variable is defined and is a number?

In the past when I needed to check if a variable was set and also a number, I would do:
if( isset($_GET['var']) && is_numeric($_GET['var']) )
But I think that's kind of ugly, especially when I need to check a bunch of variables in the same if statement, so I made a function:
function setAndNum($var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
The problem is that when I pass an undefined variable to the function, like this (supposing the variable in the GET array is undefined):
if( setAndNum($_GET['var']) )
I get the php error:
Notice: Undefined index: ...
So the whole purpose of the function is basically defeated (or half the purpose, at least ;) ).
One thing that confuses me is how the isset() function works, and why I can pass an undefined variable to it but not to my own function?
Is it possible to make my setAndNum() function work?
Your problem is with $_GET being an array. When you pass $_GET['var'] to your function, this array value is already looked up and used as an argument to the function. Therefore you cannot effectively check the presence of 'var' in $_GET from within this function. You could rewrite it a bit to make it work for array values, something like this:
function setAndNum($key, $array)
{
if(array_key_exists($key, $array) && is_numeric($array[$key]))
return 1;
else
return 0;
}
Then call it like this:
if( setAndNum('var', $_GET) )
It's good practice to verify a key exists before using it:
if (array_key_exists($_GET, 'var')) {
// do stuff with $_GET['var']
}
function setAndNum(&$var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
Please, try using this version:
function setAndNum(&$var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
You can use the # operator to prevent error reporting:
setAndNum(#$_GET['var']);
This way, the error message of the non-existant index will not be printed, and the return value will be 0.
You could also write two functions, one that checks for an array and one that checks for normal variable
function setAndNum($var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
function setAndNumArray($array, $key)
{
if(isset($array) && isset($array[$key]) && is_numeric($array[$key]))
return 1;
else
return 0;
}
if you are using variables from GET or POST method you may do like this as these are super globals.
function setAndNum()
{
if(isset($_GET['var']) && is_numeric($_GET['var']))
return 1;
else
return 0;
}
now coming to your another query. isset checks whether a variable is s et or not like
if(isset($_POST['submit']))
{
// any code under button click
}

How to convert the ASP code snippet to PHP?

function isChongHao(ary,i,j)
if ary(i-1,j)<>0 or ary(i+1,j) then
isChongHao=true
exit function
end if
isChongHao=false
end function
function isChongHao($ary, $i, $j) {
if($ary[$i-1][$j] != 0 || $ary[$i+1][$j]) {
return true;
}
return false;
}
--I suppose $ary contains a name of a function.--
Ooops, strike that: Joel thanks, I totally missed the fact that it was vbscript!!! O.o
Now maybe it is better...
function isChongHao($ary, $i, $j)
{
return ($ary[$i-1][$j] || $ary[$i+1][$j]);
}
You should probably check to ensure those indexes are set in the array beforehand, though, with an isset() call.
Assuming ary is an array
function isChongHao($ary,$i,$j) {
return (($ary[$i-1][$j] || $ary[$i+1][$j]) ? true : false);
}
Assuming ary is a function
function isChongHao($ary,$i,$j) {
return (($ary($i-1,$j) || $ary($i+1,$j)) ? true : false);
}

Categories