I have problem with variable in function - I define variable outside the function, increments the var in function and displays var outside function.
My code already looks like this:
$count = 10;
$decrease_count = function() use (&$count) {
$count--;
};
$decrease_count();
echo $count;
$added_records = 0;
Excel::filter("chunk")->selectSheetsByIndex(0)->load("storage\XYZ.xls")->chunk(250, function($reader) use (&$added_records, $part_catalogs, $parts, $cars, $car_models, $brands, &$importHelper) {
$added_records++;
echo "ADDED RECORDS LOCAL: " . $added_records . "<br>";
});
echo "<h2>Added " . $added_records . " records</h2>";
echo ADDED RECORDS LOCAL returns 1, so the $added_records++ works
Results: echo $count returns 9, but echo "Added " . $added_records (...) returns 0...
How to increment this $added_records variable??
Related
Need to create function that will call other built-in and custom functions, problem is that each called function has different number of parameters.
I supply to a callerFunction required function name and required parameters in array, but how do i implement the actual function call?
P.S. I am aware of function scope, this is not a question now.
function myFunction(inputStr, paramOne, paramTwo) {
echo inputStr . " P1: " . paramOne . " P2: " . paramTwo;
}
function callerFunction(functName, functArgsArr) {
$myVar = "";
n = 1;
while n <= functArgsArr.length {
myVar = myVar . " " . functArgsArr[n];
n++;
}
%functName%("Hello World!", %myVar%);
}
callerFunction("myFunction", ["one", "two"]);
You could use the call_user_func_array PHP function:
<?php
function myFunction($inputStr, $paramOne, $paramTwo)
{
echo $inputStr . " P1: " . $paramOne . " P2: " . $paramTwo;
}
function callerFunction($functionName, array $functArgsArr)
{
// Prepends 'Hello World' to args array
array_unshift($functArgsArr, "Hello World");
// Calls $functionName passing the values of $functArgsArray as arguments
call_user_func_array($functionName, $functArgsArr);
}
callerFunction("myFunction", ["one", "two"]);
Working PHP Fiddle
i'm using codeigniter.I want to insert data in every iteration to database.
controller
$fee=500;
$trans_fee=300;
$ins_arr=array(2,3);
$ins_array_count=count($ins_arr) ;
if(!in_array('1', $ins_arr))
{
for($i=0;$i<$ins_array_count;$i++)
{
$ins.'_'.$ins_arr[$i]= ($fee+$trans_fee);
$ins_sum+= $ins_.$ins_arr[$i];
}
}
i want to get the data inside this variable like( $ins_2 and $ins_3) and insert the value of $ins_2 and $ins_3 into db
I got value of $ins_sum correctly
Anyone please answer me
Wrap them in {}:
Using ${} is a way to create dynamic variables, simple example:
${'a' . 'b'} = 'hello there';
echo $ab; // hello there
So,
$fee = 500;
$trans_fee = 300;
$ins_arr = array(2,3);
$ins_array_count=count($ins_arr) ;
$ins_sum = 0;
if(!in_array('1', $ins_arr))
{
for($i=0;$i<$ins_array_count;$i++)
{
//$ins_.$ins_arr[$i] = ($fee+$trans_fee);
${"ins_" . $ins_arr[$i]} = ($fee+$trans_fee);
$ins_sum += ${"ins_" . $ins_arr[$i]};
}
}
echo $ins_2; //result of ins_2 = 800
echo "<br />";
echo $ins_3; //result of ins_3 = 800
echo "<br />";
echo $ins_sum; // total ins sum = 1600
I am using a function in a separate file, that gets should return me an array containing the enum values of a field:
<?php
function getEnumValues($table, $field) {
$enum_array = array();
$query = "SHOW COLUMNS FROM '{$table}' WHERE Field = '{$field}'";
$result = mysqli_query($query);
$row = mysqli_fetch_row($result);
preg_match_all('/\'(.*?)\'/', $row[1], $enum_array);
return $enum_array;
}
?>
and in the main file, I do this:
<?php
require 'common.php';
for ($i=0; $i <= 5; $i++) {
getEnumValues(property, value_type);
echo "<input type='radio' name='tipo_valor' value='$enum_array[$i]' required>" . ' ' .$enum_array[$i] . '<br>';
}
?>
The problem is that the function returns nothing.
So is the function ok for what I need?
And can I use a variable returned in another file, as a local variable?
Thanks for your time, and suggestions!
The function is returning a value; you're just not capturing it. The $enum_array in your function body only lives in the scope of that function. You need to put the return value of the function in a variable in scope. In your main file:
<?php
require 'common.php';
for ($i=0; $i <= 5; $i++) {
$enum_array = getEnumValues(property, value_type);
echo "<input type='radio' name='tipo_valor' value='$enum_array[$i]' required>" . ' ' .$enum_array[$i] . '<br>';
}
?>
I am creating simple calculator. I am just learning php and this is a small project.I have created a calculator with two inputs but I am now testing it with just one. It works but only if you type number+number. It doesn't work if it is number+number+number.
I would like that it would work if you inputted 2+2+2... or 2*2*2... or 6-2-2... and 2/2/2...
Code:
// Create Variables
$y = $_POST["input1"];
// Echo input value on screen
echo "<p>Operation: " . $y . "</p>";
// Validation
if(empty($y)){
?>
<script>
$(document).ready(function(){
$('#error').append('Error: Your Input is empty');
});
</script>
<?php
}elseif(preg_match("/[a-zA-Z]/", $y)){
?>
<script>
$(document).ready(function(){
$('#error').append('Error: You can only input numbers');
});
</script>
<?php
}else{
// Calculation Brain FOR + Operator
if (strpos($y,'+') !== false) {
$omega = substr($y, 0, strpos($y, '+'));
$alpha = substr($y, strpos($y, '+') + 1);
echo "<p>Omega: " . $omega . "</p>";
echo "<p>Alpha: " . $alpha . "</p>";
$gamma = $omega + $alpha;
// The Sum FOR + operator
echo "Calculation: " . $gamma;
}
}
That's usually a bad practice, but here you can use eval. But you have first to check that your string doesn't contains disallowed characters.
$allowedCharacters = "0123456789./*-+()% ";
if(preg_match('/^[^'.preg_quote($allowedCharacters).']+$/'), $y) {
eval('$result = '.$y.';');
echo "Calculation: " . $result;
}
The only problem is that you'll not be able to handle errors.
you could use this for the same operation
$test='2+2*3';
eval('$calc = '.$test.';');
echo "Calculation: " . $calc;
the out should be : 8
I have the following code with the if...else statement within a while loop.
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$nextcolr = next($colour);
if ($nextcolr == FALSE)
{
reset($colour);
echo current($colour);
}
else
{
echo next($colour);
}
I can't work out why what ever is in the else statement isn't being executed, even if I switch the two statements and reverse the operator. Could anyone help me?
The entire while loop:
while($row = mysql_fetch_array($result))
{
echo "by <a href='/neuro/profile.php?userid=$row[MemberID]'>" . $row['FirstName'] . " " . $row['LastName'] . "</a> on " . $row['Timestamp'] . " | " . $row['NumberOfComments'] . " comments.";
echo "<div id='blog' style='background-color:#";
if ($nextcolr == FALSE)
{
reset($colour);
echo current($colour);
}
else
{
echo next($colour);
}
echo "'><a href='blog.php?threadid=" . $row['tID'] . "'>" . $row['Title'] . "</a></div>";
}
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
while ... {
$nextcolr = next($colour);
if ($nextcolr === FALSE)
{
reset($colour);
}
echo current($colour);
}
is how your while loop should look like. If I am right, you are also defining $colour in the while loop, which might cause problems.
If all this is in the while loop, then you are re-declaring the array on each iteration, thus returning the array internal pointer to the beginning with each iteration.
If you want to iterate this array multiple times, you could do it this way:
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$i = 0;
while ... {
...
echo $colour[$i++ % count($colour)];
...
}
So you don't need this if-else block.
The problem with your original while loop is that you never change the value of $nextcolr.
Thus, it always remains FALSE and the else part never gets executed.