I am using the following code to return data from a loop and although I am receiving the data it seems there may be null data also which is producing errors.
Here is the code
$extensions = count($cert['tbsCertificate']['extensions']);
for ($j = 0; $j < $extensions; $j++) {
$count = count($cert['tbsCertificate']['extensions'][$j]['extnValue']);
for ($i = 0; $i < $count; $i++) {
if (array_key_exists('dNSName', $cert['tbsCertificate']['extensions'][$j]['extnValue'][$i])) {
$value = $cert['tbsCertificate']['extensions'][$j]['extnValue'][$i]['dNSName'];
$item = $i;
echo 'DNSName',$item,' : ', $value,"\n";
}
}
}
Here is the result of output
Can someone help me update the code to check for null values (i think in the first for loop) and ideally ignore those so the warnings don't appear? I am happy to provide more context if required.
Change your if condition so that it checks if it is set and if it is array first:
if(isset($cert['tbsCertificate']['extensions'][$j]['extnValue'][$i])
&&
is_array($cert['tbsCertificate']['extensions'][$j]['extnValue'][$i])
&&
array_key_exists('dNSName', $cert['tbsCertificate']['extensions'][$j]['extnValue'][$i]))
if it detects in first condition it is not set or is not an array, it will not proceed to array_key_exists function.
The error probably is, because the incoming data has not indexes like 0,1,2,3... - may be it starts from 1.
The easest solution I think is if you replaces FOR structures by FOREACH.
Related
In a PHP framework which provides hookpoints that are implemented using eval() I am trying to interrupt or continue a loop from within an eval() call.
This is what I am trying
The framework loops like
...
for( $i = 0; $i < 10; $i++ ) {
// framweork code here ...
eval( $plugin_code );
// framweork code here ...
}
...
$plugin_code contains PHP-code - in this sample case
if( $i == 5 ) {
continue;
}
It results in this error
PHP Fatal error: 'continue' not in the 'loop' or 'switch' context
If it is true that eval() only evaluates expressions and can NOT evaluate statements - then how can I implement the continue / break statements inside an eval()?
Leaving aside the mechanics of eval and continue for a moment, I think there is a more fundamental point to make: when writing code that "plugs in" to another system, you can only do what that system allows you to do.
If the hook system simply executes the code you give it (whether via eval, or a callback function, or any other mechanism), you cannot use it to control the flow of loops etc in the main framework code.
If the framework wanted you to do that, it would have to provide a mechanism for your plugin to signal back to the framework what you want it to do - you might register the plugin in a particular way, return a particular value from the callback, set a particular variable, etc.
If you don't want to directly modify the framework, your only option is to request such a feature from the framework's author.
You can't use a statement like continue or break in eval() to affect an outside loop.
I suggest using a variable.
$plugin_code = 'if( $i == 5 ) {
$continue = true;
}';
$continue = false;
for( $i = 0; $i < 10; $i++ ) {
eval( $plugin_code );
if ($continue) {
continue;
}
// other code
}
If you also want to skip the rest of the plugin code, it should put that code in an else block.
$plugin_code = 'if( $i == 5 ) {
$continue = true;
} else {
// other code
}';
IMHO, this whole thing smells fishy. The plugin is dependent on the specific variable being used for the loop, which is quite fragile.
Keywords like continue, break and return which affect the control-flow of the program cannot be used directly inside eval to achieve the result you want. eval is a function, and a function cannot change the control-flow of the code which calls it, except by throwing an exception.
Since continue simply means "skip to the next iteration of the loop", and this is equivalent to "don't execute the rest of the code in this block", you could rewrite your code to make the rest of the block conditional on the if statement instead. If the code looks like
for( $i = 0; $i < 10; $i++ ) {
if( $i == 5 ) {
continue;
}
// do more things
}
then this can trivially be rewritten as
for( $i = 0; $i < 10; $i++ ) {
if( $i != 5 ) {
// do more things
}
}
and this is now in a form which can be eval'd:
// your code
$plugin_code = 'if( $i = 5 ) {
// do more things
}';
// framework code
for( $i = 0; $i < 10; $i++ ) {
eval($plugin_code);
}
However this still only affects the control-flow of the code you're passing into the framework. It's not possible to change the control-flow of the framework code itself.
Say I have $exampleVariable, which I want to print. $exampleVariable may be an array, in which case I have this set up to get the right array element, which I can then print with print $exampleVariable[$i].
if ($_GET) {
$i = array_search($_GET["exampleQueryString"], $exampleVariable);
} elseif (is_array($exampleVariable)) {
$i = 0;
} else {
$i = "";
}
My problem is that last else, if $exampleVariable is NOT an array, because then I get print $exampleVariable[] which doesn't work. So is there something I can put as $i to print the whole variable?
Alternatively, I considered including the brackets in $i, so I'd have for example $i = [0];, but in that case I don't know how I'd print it. $exampleVariable$i certainly won't work.
I have a good number of variables besides $exampleVariable I'll need to print, all with the same $i or lack thereof, so I'd like to not have to do anything longwinded to set each of them up individually.
This sounds way more complicated than I feel like it should, so hopefully it makes sense!
You can always do a nifty thing that is called type casting. That means, that you can always make a variable an array even if it is not, by prepending its name by (array):
$exampleVariable = (array)$exampleVariable;
So you don't need three if branches at all:
if ($_GET) {
$i = array_search($_GET["exampleQueryString"], $exampleVariable);
} else {
$i = 0;
$exampleVariable = (array)$exampleVariable;
}
You could apply the (array) cast, which will have no effect if the target is already an array:
$i = array_search($_GET["exampleQueryString"], (array)$exampleVariable);
I've looked all over and I'm starting to wonder if what I'm wanting to do is possible. I've seen the loop to allow you to auto increment a number up to a cap:
for ($n = 0; $n <= 7; $n++)
However, what I'm trying to do is to add a auto-incrementing suffix to a variable that is already part of a loop coming from an html form. But I can't figure out how to get the autoincrement to work since I can't put a for() inside a foreach(). Let me see if I can give you an idea of what I'm looking for.
foreach($_POST[‘input’] as $input) {
${‘input.‘n++’}=$input
}
With the goal being that there will now be variable $input1, $input2, etc, each declared for an input received. Is this achievable?
$n=0;
foreach($_POST[‘input’] as $input) {
$varname = "input$n";
$$varname = $input;
$n++;
}
echo $input1;
Let's say I have this inputs :
<input type="hidden" name="block-1" value="001"/>
<input type="hidden" name="block-2" value="012"/>
<input type="hidden" name="block-3" value="002"/>
<input type="hidden" name="block-4" value="005"/>
<input type="hidden" name="block-5" value="008"/>
and I want to process those input using this PHP loop
$i = 1;
do {
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
$i++;
}
while (!empty($webBlock));
why I always have 6 outputs? and the last one is blank output. seems that loop always doing n+1. how to make correct loop based on number of inputs given? thanks!
Do you need while?
I'd go with:
$i=0;
foreach($_POST as $name => $value)
{
if( strpos($name , 'block-') !== false ) echo $i . " - " . $name . ": " . $value;
$i++;
}
Believe that should account for items named 'block-n'. The if statement basically says "if block- is anywhere in the name of the field, echo out such and such".
Let me know if you get an error and will amend.
Because you are using a repeat loop, you should use a while loop:
while (!empty($webBlock)){
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
$i++;
}
beacause do will get executed at least once what ever may be in while expression .do while is an exit control loop.
Try this:
$i = 0;
do {
$i++;
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
}
while (!empty($webBlock));
UPD: The best approach is this:
for ($i = 1; $i <= count($_POST); $i++) {
$webBlock = $_POST['block-'.$i];
//some code here
}
Because You are using Do While loop.I am not sure about your data but i must say that it will execute one more time as you expected.Let us check it by iterating
It will run first time because $webBlock is not empty and same as for 5 times.Now here you want to close the loop but your 5th iteration's condition gets true.Now it will execute once more and show nothing (because there is nothing in $webBlock)and now condition will find that $webBlock is empty.I suggest you to use while loop here.It will solve your issue
I am not a PHP developer so I will just give you general idea about what you are doing.
Actually you are using Do-while loop which is sometimes called Exit Control loop. So, in case of Do-While after executing code your condition will be checked. So, even you didn't get any value in 'webBlock' your code will be executed. So, this is your bug.
Instead of this you can use While loop, Entry control loop. You code is executed if and only if condition is true.
$i = 1;
while (i>0) {
$x = 'block-'.$i;
$webBlock = $_POST[$x];
if(empty($webBlock))
{
break;
}
$i++;
}
i got a code of 100-200 rules for making a table. but the whole time is happening the same.
i got a variable $xm3, then i make a column . next row, i got $xm2 and make column. next row, i got $xm1 and make column.
so my variables are going to $xm3, $xm2, $xm1, $xm0, $xp1, $xp2, $xp3.
is there a way to make a forloop so i can fill $xm and after that a value from the for loop?
In this kind of structure you'd be better off using an array for these kinds of values, but if you want to make a loop to go through them:
for($i = 0; $i <= 3; $i++) {
$var = 'xm' . $i
$$var; //make column stuff, first time this will be xm0, then xm1, etc.
}
It is not fully clear what you are asking, but you can do
$xm = 'xm3';
$$xm // same as $xm3
in PHP, so you can loop through variables with similar names. (Which does not mean you should. Using an array is usually a superior alternative.)
As far as I am aware using different variable names is not possible.
However if you uses arrays so as below
$xm[3] = "";
$xm[2] = "";
$xm[1] = "";
$xm[0] = "";
or just $xm[] = "";
Then you can use a for each loop:
foreach($xm as $v) { echo $v; }
Edit: Just Googled and this is possible using variable names but is considered poor practice. Learn and use arrays!
You can do this using variable variables, but usually you're better off doing this sort of thing in an array instead.
If you're positive you want to do it this way, and if 'y' is the value of your counter in the for loop:
${'xm' . $y} = $someValue;
You can easily do something like this:
$base_variable = 'xm';
and then you can make a loop creating on the fly the variables;
for example:
for ($i=0; $i<10; $i++)
{
$def_variable = $base_variable . $i;
$$def_variable = 'value'; //this is equivalent to $xm0 = 'value'
}