Is a string in PHP an array, or not? - php

I need to ask that what is string in PHP. Is it an array in PHP or not. Please give true justifications.

A string in PHP is essentially a byte array (but not in the sense of a PHP's "array"); i.e., it's a buffer with only one piece of meta-data -- the size of the buffer.
An array in PHP is a double-linked hash table map, where the keys can be integers, strings, or a mixture of both.
In terms of PHP's type system, strings and arrays are two of the basic types.

You can read the documentation about php strings at
http://www.php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.types.array.php

In PHP, a string is a primitive type, meaning it's not an array. See here for the other primitive types supported by PHP.

Related

Is it okay to represent JSON [] and {} as different values in PHP?

I'm writing data and string parsers for a JSON variant. My understanding is that standard JSON is incompatible with PHP, since PHP uses the same datatype (Array) for JSON arrays and JSON objects. This problem is severe with empty JSON arrays and objects, since once converted to a PHP value, they are indistinguishable. So if you start with JSON strings "[]" and "{}", converting them to values and then back to JSON makes them look the same.
My basic idea is to require PHP arrays intended to be represented as JSON objects, especially empty arrays that should be encoded or stringified to "{}", to be represented differently in PHP programs, so proper error checking and JSON conversion can be done. Of course, for nonempty PHP arrays, it is possible to determine programmatically whether they have indices that are successive integers starting at 0 or not. But such a check fails to be useful if empty JSON arrays and objects are also to be properly distinguished.
The actual distinction discussed here is important to choose well, as programmers will be required to generate and type-check PHP Arrays differently depending on whether they are intended to correspond to "[]" or "{}" syntax in JSON.
So, my question is: what distinctive representation is best? The main candidates are:
for PHP associative arrays representing JSON objects:
$JSONobject=(object){};
$JSONobject=new stdClass();
$JSONobject=json_decode("{}");
I list these separately as I am not sure if they all have the same internal representation.
For PHP sequential list arrays representing JSON arrays, the Array datatype would be used unchanged, so an empty JSON array would be generated by $JSONarray=[]; or $JSONarray=new Array();
I have an answer to my own question, as a result of experimentation: I've found that json_decode("[]") results in a zero-length array, and json_decode("{}") results in a standard class object with no properties, no inheritance, and no methods. Furthermore, json_encode(json_decode ("[]") results in "[]", which is correct, and json_encode(json_decode ("{}") results in "{}", which is also correct.
So PHP itself chose my proposed answer to disambiguate the PHP Array type into two separate subtypes (Array and Class Object).
I think this answers my question in the affirmative. Furthermore, the three ways I listed above for representing JSON "{}" are indeed equivalent, as I guessed.
I hope this question and answer help those who may be puzzled by this issue in the future. The ambiguity of using Array to represent both lists and associative maps is permitted (and probably recommended) to be resolved in just the way I described.

What is the name of this structured string?

In mysql I can see a lot of these structured strings (it's like json but not really):
a:2:{s:5:"hello";s:0:"";s:7:"World !";s:0:"";}
Does it have a special name?
It is possible to recalculate automatically the length of strings (after search and replace)?
This is a serialized string in PHP - check out the serialize() and unserialize() functions in the documentation:
http://php.net/manual/en/function.serialize.php
You can't really edit them inline (at least I wouldn't recommend it) - your best bet would be to userialize(), make the change to the array, and then re-serialize() the array.

php - associative array index naming conventions

In PHP, do associative array indexes need to follow that same rules and variable names (can't start with a number, etc.) I am looking for both working and philosophical answers to this question.
From the manual:
A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.
In their example, using something like $array["08"] is perfectly acceptable and will count as a string, though as you ptobably know, it's highly not recommended. Always name your variables logically.
no, associative arrays can have numerical keys. any valid string can be an index. as far as code styles and clarity, the important thing is that is the keys make sense and are readable.
as far as convention goes, often to differentiate between variable names and indexes I've seen people use lowercase letters and underscores. Although tedious, I find it increases readability because the eye expects a small-case index for an array named usually with one word: array['array_index'] looks good; array['arrayIndex'] is often harder to read in some code.
An array key can be an integer or any valid string, according to the manual.
From a philosophical standpoint, the key should make sense in context and add to the readability of the code.
No, they can be any string, even a binary one.

PHP: JSON like data format, searching parser

I would like to have an JSON like data format with the following features:
support of arrays (ideal with [item; item; item] as notation and [key: value; key2: value2]), also nested
string support, ideal with the following: "foo",0x0a,"bar"
hex numbers, bin numbers, decimal numbers
Does anyone know a parser for such a data format written in PHP?
JSON is too inflexible, because keys must be in quotes and associative arrays have a different notation from normal ones and because of the missing string concatenation for special characters.
Take a look at YAML. It should come fairly close to what you want. It has lists and associative arrays, knows a number of data types. Support for hexadecimal numbers seems to depend on the parser used.
It doesn't seem to do binary numbers, and can't do string concatenation as outlined in your example, but a well-written parser should be relatively easy to extend accordingly. Also, there is a provision for user-defined data types.
Here is a list of PHP YAML parsers.

Bit conversion operations in PHP

I find myself in need of performing bit-level conversion on variables in PHP. In more detail, I have a bit stream that is read as an integer by hardware, and I need to do some operations on the bits to make it into what its actually supposed to be (a float). I have to do this a few times for different formats, and the functionality I need is
Being able to select and move individual bits in a variable
Being able to cast statically one type of variable to the other (ie. int to float)
I know php natively supports bitwise AND, OR, etc, and shift operations, but I was wondering if:
there may already be a library in php that does this sort of thing
I would be better off with delegating the calculations to some other language
Thanks,
Is it pack and unpack that you want?
For converting bit stream to whatever you need use unpack().
Once converted to integer, you can use bitwise operators. Note, that they don't support floats, so in case of float it'd first be casted to integer.
http://php.net/manual/en/language.operators.bitwise.php

Categories