Making an image gallery but why do I have this error? - php

I'm having some errors with my file. Does anyone know what I did wrong?
Error:
syntax error, unexpected ':', expecting ')' in C:\wamp65\www\Php2\01_gallery.php on line 5
It gives above error when I load the page and the error comes from this line:
$arrayImages=glob( pattern: $path."/*.{".$extentions."}", flags: GLOB_BRACE);

The problem is that you are using the PHP 8 syntax but you are trying to execute this code with PHP 7. This syntax is invalid before PHP 8, so you need to make sure that you are using PHP 8.
Named arguments were introduced in PHP 8 and they allow you to provide arguments out of order because each argument is named e.g. pattern: $thisIsMyPattern.
If you can't use PHP 8 for the moment for some reason then a workaround is to remove the parameter names, which you don't even need in this example.
$arrayImages = glob($path."/*.{".$extentions."}", GLOB_BRACE);

Related

How to fix Syntax error, unexpected '' (T_STRING)?

The error that I am receiving is this: Parse error: syntax error, unexpected 'TokenType' (T_STRING) in C:\Program Files\Ampps\www\cs5339\lepichardo\4339_f22_assignment1\TokenType.php on line 2. To be clear, both files are php, and they both are in the same director. The code below is where I include the enum TokenType so that I would be able to use it.
<?php
include 'TokenType.php'
?>
The TokenType.php looks like this
<?php
enum TokenType{
case INT;
case STRING;
case COMMA;
}
?>
As far as I now, this should be fine, but gives me that error in that particular line.
If there is any other way to declare and imlpement the enum variable in php I would like to be explained to how to. Thanks in advance!!!
enums are only available in PHP 8.1+. The error suggests you are running PHP 7.
Here's some results of running the following code using various PHP versions (using https://3v4l.org/qHU4f)
<?php
enum TokenType{
case INT;
case STRING;
case COMMA;
}
echo 'hello world';
Output for 8.1.0 - 8.1.11, 8.2rc1 - rc3
hello world
Output for 8.0.1 - 8.0.24
Parse error: syntax error, unexpected identifier "TokenType" in /in/qHU4f on line 2
Process exited with code 255.
Output for 7.4.0 - 7.4.32
Parse error: syntax error, unexpected 'TokenType' (T_STRING) in /in/qHU4f on line 2
Process exited with code 255.
As you can see by the last result, unexpected 'TokenType' (T_STRING) is an error you would receive in PHP 7

PHP version 4 :: Parse error: syntax error, unexpected '=', expecting ')'

This function working properly with the newer PHP versions:
function MKDSLD($mkD,&$slD=array()){}
BUT
I'm getting :
Parse error: syntax error, unexpected '=', expecting ')'
With PHP 4.
Needless to say that I got to solve it under the older version of PHP :)
Pretty simple but I guess small tweak is needed here :D
Thank you in advance !
PHP 4 doesn't support default parameters for reference parameters.
You should deal with $slD as a required argument:
function MKDSLD($mkD, &$slD){
// code
}
$x = array();
MKDSLD(5, $x);

PHP Parse error: syntax error, unexpected '[' [duplicate]

This question already has an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 7 years ago.
I got few lines producing error 500. The error log says:
PHP Parse error: syntax error, unexpected '[' in...
and the lines are:
$catids[] = $params->get('catid');
and after commenting it out, this one comes up:
$return_category[] = self::_getCategoryInfor($tg, $params)[0];
How can i modify them in order to match with new PHP versions and solve the error 500?
From your code, I do not see code that may triggered this parse error. Try change your code so it becomes:
$categories = self::_getCategoryInfor($tg, $params);
$return_category[] = $categories[0];
This is the expression causing the error:
self::_getCategoryInfor($tg, $params)[0];
Function array dereferencing was added in PHP 5.4. Your code will generate a parse error on 5.3 or lower:
https://secure.php.net/manual/en/migration54.new-features.php

Trying to use HUE api but getting php syntax error

I am currently trying to use the https://github.com/muesli/huephp to control my lights through PHP but I am running into a syntax problem.
Here is the line in huecli.php that I am getting a syntax error on where [...
Here is the actual error: "Parse error: syntax error, unexpected '[' "
$hue->lights()[$light]->setLight( $command );
There are more errors but it seems the [$light] is causing the problem. I haven't worked with this kind of syntax before so any help is appreciated!
You are probably not running PHP 5.4+ which is what is required to use Array Dereferencing. To use this code it must be modified as so:
$hue_lights = $hue->lights();
$hue_lights[$light]->setLight( $command );

Why do I have to assign a function return (Array) to a variable before using it?

When the following line is reached:
$_SESSION['LOGIN'] = good_query_table($login_query)[0]["name"];
This error is printed:
PHP Parse error: syntax error, unexpected '[', expecting ',' or ';' in /srv/web/login.php on line 6
Attempting to find out what's wrong since this code has been running fine for years, I edited the code to first assign the function returned array to an array variable, then use it, which ran fine afterwards !
$arr = good_query_table( $login_quert );
$_SESSION['LOGIN'] = $arr[0]["name"];
The only new thing is that I'm now running the application on Ubuntu. It used to run for years on Windows.
So why do I have to do that ?
What version of PHP is each server running?
Function array de-referencing was added in 5.4.
http://www.php.net/manual/en/migration54.new-features.php

Categories