Set function value based on variable - php

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);

Related

php array_filter is filtering too much

Here is the code :
<?php
$a_campagnes = $this->campagne->get_campagnes_client();
foreach($a_campagnes as $o_camp){
if($o_camp->groupes){
foreach($o_camp->groupes as $o_groupe){
if($o_groupe->IDGroupe == $this->session->o_user->IDGroupe){ echo 'ok';}
}
}
}
$a_campagnes = array_filter($a_campagnes, function($o_camp){
if($o_camp->groupes){
foreach($o_camp->groupes as $o_groupe){
if($o_groupe->IDGroupe == $this->session->o_user->IDGroupe) return true;
}
}
return false;
});
$a_campagnes contains at first 10 objects
The result of the first foreach is okokokok
The result of $a_campagnes after the array_filter (which is the same code as the first foreach) is null
Where are the four objects matching my first foreach?
EDIT
Just tried that piece of code:
$i_id_groupe_user = $this->session->o_user->IDGroupe;
foreach($a_campagnes as $o_camp){
if($o_camp->groupes){
foreach($o_camp->groupes as $o_groupe){
if($o_groupe->IDGroupe == $i_id_groupe_user){ echo 'ok';}
}
}
}
$a_campagnes = array_filter($a_campagnes, function($o_camp) use ($i_id_groupe_user){
if($o_camp->groupes){
foreach($o_camp->groupes as $o_groupe){
if($o_groupe->IDGroupe == $i_id_groupe_user) return true;
}
}
return false;
});
It gives the same result as before
$this doesn't exist inside anonymous functions, and you're trying to use it as if it was inside your class scope, which would be even less logical.
If you want to use whatever $this->session is inside your array_filter() callback, you'll have to either declare a class method specifically for that, or tell the anonymous function that it can use it, like this:
$session = $this->session;
$a_campagnes = array_filter($a_campagnes, function($o_camp) use ($session) {
if ($o_camp->groupes) {
foreach($o_camp->groupes as $o_groupe) {
if ($o_groupe->IDGroupe == $session->o_user->IDGroupe) return true;
}
}
return false;
});

Recursive function acts weird in php

I got stuck when I learned about recursive functions in PHP. I know recursive function are those which are being called by itself. My code is:
function addition_no($x,$y) {
if($x==0) {
return $x;
}
return addition_no($x+$y);
}
echo addition_no(1,2);
When I tried executing this code I get:
Warning: Missing argument 2 for addition_no(), called in
/web/com/13978781902261/main.php on line 6 and defined in
/web/com/13978781902261/main.php on line 2
What i need is to add two numbers via recursion.
I created this code in order to summarize values in an array using recursivity is:
<?php
//values to add
$add = array(5,4,4,6,7);
function addval($add,$pos) {
if($pos == 0) {
return $add[0];
}
else {
return $add[$pos] + addval($add,$pos-1);
}
}
//last position in vector
$last_position = count($add)-1;
echo addval($add, $last_position);
?>
You can try this
return addition_no($x,$y);
You have to check any condition for recursive function otherwise it will be infinite recursion..
The warning is appropriate as you call the function inside it self with a different argument structure.
Try the factorial recursive function to learn how it works:
<?php
// Initiating Recursion
echo factorial(5);
//Recursive Function Definition with 1 parameter
function factorial($number)
{
//Break condition for recursion
if($number==1)
return $number;
//Fetch and Stack Recursion
return $number*factorial($number-1);
}
So, you get the basic recursion, in simple English, as :
factorial(n) = n x factorial(n-1) = n x (n-1) x factorial(n-2)
For You particular case, you actually don't need a recursion as your arguments are not relying on the previous argument result. However, for the sake of learning, you can do something like this:
<?php
//Summation
function summation($x) {
if($x==0) {
return $x;
}
return $x+summation($x-1);
}
echo summation(5);
//Summation with 2 step
function summation($x,$y) {
if($x<1) {
return $x;
}
return $x+summation($x-$y,$y);
}
echo summation(5,2);

Calling a PHP function by reference behaving unexpectedly

Iwonder why the out put is 0(zero) for below code snippet? Can anyone please clarify why below code output is zero.
<?php
function a($number)
{
return (b($number) * $number);
}
function b(&$number)
{
++$number;
}
echo a(5); // output 0(zero) ?
?>
You never return any value from the function, and you're trying to echo the return value.
function b(&$number)
{
return ++$number;
}
Note that this is a silly example for a function that takes its parameter by reference, since you don't have a reference to the original value 5. Something like this would be more appropriate:
function b( &$number) {
++$number;
}
$num = 5;
b( $num);
echo $num; // Prints 6
The function name is b, but you are calling a...
Also, you are echoing a function, that doesn't return a value. This means you are echoing a non-initialize variable.
You must either return a value:
return ++$number;
or echo the variable directly:
$number = 5;
b($number);
echo $number;

float Number validation check for codeigniter

Here I am entering tax field in % but when i enter values like 2.5,0.5 other than integer it is generating error.
Here is my code for Validation,any idea for entering float numbers
function _set_rules()
{
$this->form_validation->set_rules('pst','PST','trim|required|is_natural|numeric|
max_length[4]|callback_max_pst');
$this->form_validation->set_rules('gst','GST','trim|required|is_natural|numeric|
max_length[4]|callback_max_gst');
}
function max_pst()
{
if($this->input->post('pst')>100)
{
$this->form_validation->set_message('max_pst',' %s Value Should be less than or equals to 100');
return FALSE;
}
return TRUE;
}
function max_gst()
{
if($this->input->post('gst')>100)
{
$this->form_validation->set_message('max_gst',' %s Value Should be less than or equals to 100');
return FALSE;
}
return TRUE;
}
</code>
remove the is_natural from the validation rules and replace it with greater_than[0] and less_than[100]
function _set_rules()
{
$this->form_validation>set_rules('pst','PST','trim|required|
greater_than[0]|less_than[100]|max_length[4]|callback_max_pst');
$this->form_validation->set_rules('gst','GST','trim|required|
greater_than[0]|less_than[100]|max_length[4]|callback_max_gst');
}
greater_than[0] will apply numeric
From the codeigniter documentation:
is_natural Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. source
Clearly, values like 2.5,0.5 are not natural numbers so they will fail validation. You can use a callback and return the value after parsing the value with floatval() PHP function.
Hope it helps!
You can try this :
function _set_rules()
{
$this->form_validation>set_rules('pst','PST','trim|required|
numeric|max_length[4]|callback_max_pst');
$this->form_validation->set_rules('gst','GST','trim|required|
numeric|max_length[4]|callback_max_gst');
}
function max_pst($value) {
$var = explode(".", $value);
if (strpbrk($value, '-') && strlen($value) > 1) {
$this->form_validation->set_message('max_pst', '%s accepts only
positive values');
return false;
}
if ($var[1] > 99) {
$this->form_validation->set_message('max_pst', 'Enter value in
proper format');
return false;
} else {
return true;
}
}
Hope this code will help you.... :)

why does my function always return false?

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!
}
}

Categories