PHP: why i am getting this error ??? "syntax error, unexpected '{'" - php

i wanted to arrange this arrays element in assending order and wrote the below code :
<?php
$a=array("z","s","a","j","t","b");
for($i=0;$i<=6;$i++)
{
if ($i[0]<$i[1]) { echo $i[1]; }
else if ($i[1]<$i[2]) { echo $i[2]; }
else if ($i[2]<$i[3]) { echo $i[3]; }
else if ($i[3]<$i[4]) { echo $i[4]; }
else if ($i[4]<$i[5]) { echo $i[5]; }
else if ($i[5]<$i[6]) { echo $i[6]; }
else if ($i[6]<$i[7]) { echo $i[7]; }
else if ($i[7]<$i[8]) { echo $i[8]; }
else if ($i[8]<$i[9]) { echo $i[9]; }
else if ($i[9]<$i[10]) { echo $i[10]; }
else if ($i[10]<$i[11]) { echo $i[11]; }
else ($i[11]<$i[12]) { echo $i[12]; }
}
?>
but i an getting the following error :
Parse error: syntax error, unexpected '{' in C:\wamp\www\arange.php on line 16
how can i rectify it

This snippet is the problem:
else ($i[11]<$i[12]) { echo $i[12]; }
Either edit it into elseif or remove the ($i[11]<$i[12]).

I'd do this differently. Consider using PHP's built in sort() function.
$a = array("z","s","a","j","t","b");
sort($a);
foreach ($a as $element) {
echo "$element\n";
}
Also read about the foreach statement.

$b = '';
$a=array("z","s","a","j","t","b");
foreach($a as $i) if($i > $b) $b = $i;
echo $b;

Check out the manual for a clear example of the elseif/else if syntax. The else part in your code is the problem.

if ($i[5]<$i[6]) { echo $i[6]; }
actually will output something like this;
if ( b < ) { echo ; }
That why you see kinda error...

Related

PHP array values not being changed

I was trying to make a program to do synthetic division which requires factoring so I wrote this function to factor an integer which works, but it never changes the values of the php array $factors. Any help will be greatly appreciated.
$factors=array();
$i;
function factor($x){
if($x==0){
echo "(0,0)";
} else {
$n=false;
if($x<0) {
$x=abs($x);
$n=true;
}
for($i=2; $i<=$x; $i++) {
if($x%$i==0){
if($n){
$factors[(count($factors))]=(-1*($x/$i));
$factors[(count($factors))]=($i);
$factors[(count($factors))]=($x/$i);
$factors[(count($factors))]=(-1*$i);
} else {
$factors[(count($factors))]=($x/$i);
$factors[(count($factors))]=($i);
}
}
}
}
}
factor(-4);
Try this
function factor($x){
$factors=array();
if($x==0){
echo "(0,0)";
} else {
$n=false;
if($x<0) {
$x=abs($x);
$n=true;
}
for($i=2; $i<=$x; $i++) {
if($x%$i==0){
if($n){
$factors[(count($factors))]=(-1*($x/$i));
$factors[(count($factors))]=($i);
$factors[(count($factors))]=($x/$i);
$factors[(count($factors))]=(-1*$i);
} else {
$factors[(count($factors))]=($x/$i);
$factors[(count($factors))]=($i);
}
}
} //end for
echo '(' . implode(',', $factors). ')';
} //end if $x == 0
}
factor(-4);
$factors is outside the scope of the function, besides that you were not returning or outputting anything. Seeing as you echo (0,0) I assumed you wanted it echoed such as (2,4,8) etc. Which you can do with implode..

How to store $_FILES superglobal variables in an array and loop through them to find a match in PHP?

I'm running if statements to identify each variable independently. My code is a bit messy and long but it works with no problem. I'd like to see if it is possible to use an array to store them and run a loop to see if there is a match, instead of using if statements.
PHP code...
if (move_uploaded_file($_FILES["index_deslizador_Cfile1"]["tmp_name"], $index_deslC1)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile2"]["tmp_name"], $index_deslC2)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile3"]["tmp_name"], $index_deslC3)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile4"]["tmp_name"], $index_deslC4)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile1"]["tmp_name"], $index_deslM1)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile2"]["tmp_name"], $index_deslM2)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile3"]["tmp_name"], $index_deslM3)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile4"]["tmp_name"], $index_deslM4)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else {
echo "Sorry, there was an error uploading your file.";
}
I'm fairly new to PHP, so I may get scolded for this suggestion :)
If you could get $index_deslC# variables into an array, you might be able to do something like this:
$i = 1
$destination = array($index_deslC...
foreach($_FILES["index_deslizador_Mfile" . $i]["tmp_name"] as $filename) {
isset(move_uploaded_file($filename, $destination[$i]) ?
echo "<script>window.open('../index.php','_self')</script>" : echo '';
$i++;
}

How to Perform True False Comparison on Two different array values

I have two arrays:
$array_one = array(1=>6000,2=>500);
$array_two = array(1=>6500,2=>250);
I would like to compare the values with > or < like this:
if(6000 > 6500){
echo "ok";
}else{ echo "not allowed";}
if(500> 250){
echo "ok";
}else{ echo "not allowed";}
How can I perform this type of operation using a loop or something else?
You access array values using the square bracket notation [index], therefore you can simply reference the values using their index;
if($array_one[1] > $array_two[1]) {
echo "ok";
}
else {
echo "not allowed";
}
and then you can put that in a loop, like this;
for($i=1;$i<=count($array_one);$i++) {
if($array_one[$i] > $array_two[$i]) {
echo "ok";
}
else {
echo "not allowed";
}
}
Hope this helps.
Try the following:
<?php
foreach($array_one as $key => $value) {
if($value > $array_two[$key]) {
echo "OK";
} else {
echo "Not Allowed";
}
}
?>
Try this:
$array_one = array(1=>6000,2=>500);
$array_two = array(1=>6500,2=>250);
foreach($array_one as $k => $v)
{
if($v > $array_two[$k]){
echo "ok";
}else{ echo "not allowed";}
}

Is there any possibility to break a loop while calling a function?

Let's say I have a simple code:
while(1) {
myend();
}
function myend() {
echo rand(0,10);
echo "<br>";
if(rand(0,10) < 3) break;
}
This will not work with error code 'Fatal error: Cannot break/continue 1 level on line'.
So is there any possibility to terminate the loop during a subfunctin execution?
Make the loop condition depend upon the return value of the function:
$continue = true;
while( $continue) {
$continue = myend();
}
Then, change your function to be something like:
function myend() {
echo rand(0,10);
echo "<br>";
return (rand(0,10) < 3) ? false : true;
}
There isn't. Not should there be; if your function is called somewhere where you're not in a loop, your code will stop dead. In the example above, your calling code should check the return of the function and then decide whether to stop looping itself. For example:
while(1) {
if (myend())
break;
}
function myend() {
echo rand(0,10);
echo "<br>";
return rand(0,10) < 3;
}
Use:
$cond = true;
while($cond) {
$cond = myend();
}
function myend() {
echo rand(0,10);
echo "<br>";
if(rand(0,10) < 3) return false;
}

Checking for undefined variables in a function php

So I've been trying to devise a function that will echo a session variable only if it is set, so that it wont create the 'Notice' about an undefined variable. I am aware that one could use:
if(isset($_SESSION['i'])){ echo $_SESSION['i'];}
But it starts to get a bit messy when there are loads (As you may have guessed, it's for bringing data back into a form ... For whatever reason). Some of my values are also only required to be echoed back if it equals something, echo something else which makes it even more messy:
if(isset($_SESSION['i'])){if($_SESSION['i']=='value'){ echo 'Something';}}
So to try and be lazy, and tidy things up, I have tried making these functions:
function ifsetecho($variable) {
if(!empty($variable)) {
echo $variable;
}
}
function ifseteqecho($variable,$eq,$output) {
if(isset($variable)) {
if($variable==$eq) {
echo $output;
}
}
}
Which wont work, because for it to go through the function, the variable has to be declared ...
Has anyone found a way to make something similar to this work?
maybe you can achieve this with a foreach?
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$eq,$output) {
if($variable==$eq) {
echo $output;
}
else echo $variable;
}
}
now this will all check for the same $eq, but with an array of corresponding $eq to $variables:
$equiv = array
('1'=>'foo',
'blue'=>'bar',);
you can check them all:
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$equiv) {
if(isset($equiv[$variable])) {
echo $equiv[$variable];
}
else {
echo $variable;
}
}
}
Something like this?, you could extend it to fit your precise needs...
function echoIfSet($varName, array $fromArray=null){
if(isset($fromArray)){
if(isset($fromArray[$varName])&&!empty($fromArray[$varName])){
echo $fromArray[$varName];
}
}elseif(isset($$varName)&&!empty($$varName)){
echo $$varName;
}
}
You may use variable variables:
$cat = "beautiful";
$dog = "lovely";
function ifsetecho($variable) {
global $$variable;
if(!empty($$variable)){
echo $$variable;
}
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
UPDATE: With a rather complex code I’ve managed to meet your requirements:
session_start();
$cat = "beautiful";
$dog = "lovely";
$_SESSION['person']['fname'] = "Irene";
function ifsetecho($variable){
$pattern = "/([_a-zA-Z][_a-zA-Z0-9]+)".str_repeat("(?:\\['([_a-zA-Z0-9]+)'\\])?", 6)."/";
if(preg_match($pattern, $variable, $matches)){
global ${$matches[1]};
if(empty(${$matches[1]})){
return false;
}
$plush = ${$matches[1]};
for($i = 2; $i < sizeof($matches); $i++){
if(empty($plush[$matches[$i]])){
return false;
}
$plush = $plush[$matches[$i]];
}
echo $plush;
return true;
}
return false;
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
echo "<br/>";
ifsetecho("_SESSION['person']['fname']");
echo "<br/>";
ifsetecho("_SESSION['person']['uname']");
echo "<br/>";

Categories