1

on my localhost PHP7 is running and I am using define() function to define an array. It's working fine.

define('JOBS_CREATORS', ['aaa@admin.com', 'bbbb@admin.com']);

As we know it works in PHP7 version. I googled and found const keyword which works in PHP5.6 or lower version.

As I know the const works only within the class.

But I am using WordPress so I need to define in functions.php file.

Is there any other way to define an array without using class?

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
  • 1
    If your hosting provider is still on PHP 5.6, you need to think about finding a new one. Even PHP 7.0 is old enough that it's out of support now and considered end-of-life. – Spudley Mar 15 '19 at 10:15
  • Actually, there are other PHP sites are running and they require PHP lower version. That's why I can't upgrade the PHP version. – Gufran Hasan Mar 15 '19 at 10:16
  • 1
    Fix the other sites so they don't depend on unsupported versions of PHP which don't get security fixes! – Quentin Mar 15 '19 at 10:44
  • 1
    This is also why platforms like Docker are so good. With Docker you can run each of your systems in its own isolated container, so they can all run the version of PHP (or whatever) that they need, without interfering with each other. – Spudley Mar 15 '19 at 11:38

3 Answers3

1

You can update your constant to accept a serialized array instead.

define('JOBS_CREATORS', serialize(['aaa@admin.com', 'bbbb@admin.com']));

then

if(in_array("aaa@admin.com", unserialize(JOBS_CREATORS))){ ...
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
  • 1
    if you're going to do that, use `json_encode` rather than `serialize`. – Spudley Mar 15 '19 at 11:38
  • @Spudley, serialize is working fine. Is there any different to json_encode instead of serializing? – Gufran Hasan Mar 15 '19 at 11:52
  • 1
    Serialize is overweight and overcomplex (and occasionally prone to security holes). The only correct use-case for it is if you're using the sleep/wakeup functionality for persisting an object between runs. – Spudley Mar 15 '19 at 11:56
  • Thanks @Spudley for help :) – Gufran Hasan Mar 15 '19 at 11:58
  • 1
    @GufranHasan The main point was serializing the data, no matter how you do it. `json_encode` is also a form of serialization. BTW, wordpress still uses `serialize` for saving data into database. – Taha Paksu Mar 15 '19 at 13:15
0

It is still possible to do this in PHP 5.6 using the const keyword (see in this post):

PHP Constants Containing Arrays?

const JOBS_CREATORS = ['aaa@admin.com', 'bbbb@admin.com'];
Asped
  • 3,083
  • 4
  • 31
  • 52
  • Thanks for your answer. I have tried it but unfortunately, it's not worked.it' gives error const value cannot update. – Gufran Hasan Mar 15 '19 at 10:59
  • 1
    can you show me your code? Of course you cannot UPDATE a constant... that is why it is called a constant. If you need to update it, you have to use a variable instead of a constant – Asped Mar 15 '19 at 11:08
0

As @Spudley suggested that use json_encode rather than serialize. I have used json_encode and json_decode as:

Defined:

define('JOBS_CREATORS', json_encode(['aaa@admin.com', 'bbbb@admin.com']));

Getting:

echo '<pre>';print_r(json_decode(JOBS_CREATORS)); echo '</pre>';

But @TahaPaksu's answer is also useful.

Thanks, @Spudley and @TahaPaksu :)

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51