How to generate a random, unique, alphanumeric string?

PHP 7 standard library provides the random_bytes($length) function that generate cryptographically secure pseudo-random bytes. Example: $bytes = random_bytes(20); var_dump(bin2hex($bytes)); The above example will output something similar to: string(40) “5fe69c95ed70a9869d9f9af7d8400a6673bb9ce9” More info: http://php.net/manual/en/function.random-bytes.php PHP 5 (outdated) I was just looking into how to solve this same problem, but I also want my function to create a … Read more

Can’t use method return value in write context

empty() needs to access the value by reference (in order to check whether that reference points to something that exists), and PHP before 5.5 didn’t support references to temporary values returned from functions. However, the real problem you have is that you use empty() at all, mistakenly believing that “empty” value is any different from … Read more

PHP – how to create a newline character?

Only double quoted strings interpret the escape sequences \r and \n as ‘0x0D’ and ‘0x0A’ respectively, so you want: “\r\n” Single quoted strings, on the other hand, only know the escape sequences \\ and \’. So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string … Read more

json_decode to array

As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code: $result = json_decode($jsondata, true); If you want integer keys instead of whatever the property names are: $result = array_values(json_decode($jsondata, true)); However, with your current decode … Read more

How to define an empty object in PHP

$x = new stdClass(); A comment in the manual sums it up best: stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces. When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass … Read more

How can one use multi threading in PHP applications

Multi-threading is possible in php Yes you can do multi-threading in PHP with pthreads From the PHP documentation: pthreads is an object-orientated API that provides all of the tools needed for multi-threading in PHP. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Threaded objects. Warning: The pthreads extension cannot be … Read more