It is a bug of php about define? - php

Writing:
define ("MYARR", array(
'TITLE' => "MY TITLE",
) );
And giving:
print_r(MYARR);
No error is returned. It is all ok! I display:
Array
(
[TITLE] => MY TITLE
)
But if i write:
define ("MYARR['TITLE']", "MY TITLE");
I don't get a error, but giving:
print_r(MYARR);
I get:
Warning: Use of undefined constant MYARR - assumed 'MYARR'
And giving:
echo MYARR['TITLE'];
I get this two warning:
1) Warning: Use of undefined constant MYARR
2) Warning: Illegal string offset 'TITLE'
About first warning, it not is correct, becouse as is possible to see above it is declared!
I think which is a bug of PHP. From a side is allowed declare constant of array but of other side not allowed. What is your opinion?

Related

"Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated in"

I want to run the following command with exec but I am getting an error. So what should I use instead?
php /var/.../../example.php -- 123456789 exampleData1 exampleData2
Error:
Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated in /var/.../../example.php on line X
I tried this:
$argumentsArray = [
'postID' => 123456
'foo' => 'bar'
];
exec(php /var/.../../example.php -- $argumentsArray);
and;
parse_str(parse_url($argv[0], PHP_URL_QUERY),$arguments);
$postID = $arguments['postID'];
Error: Notice: Undefined index: postID in..
You can't substitute an entire array into a string. Use implode() to convert an array into a string with a delimiter between the values.
$argumentsArray = [
'postID' => 123456
'foo' => 'bar'
];
$arguments = implode(' ', $argumentsArray);
exec("php /var/.../../example.php -- {$arguments}");

PHP creating multidimensional array errors

I want to create a multidimensional array on my code. I wrote something like below:
$game[$game_id]['map'][$place][0]
But I'm getting these errors:
-Illegal string offset 'map'
-Uninitialized string offset
-Undefined offset
Last 2 errors are for $place.
What's wrong?
You cant create the whole array in one step.
You get these errors, because $game[$game_id] seems to be an string and not an array. So ['map'] tries to accesss the 'map' index of an string which causes the Illegal string offset warning. The other errors are just an result of this, because $game[$game_id]['map'] returns null(and the warning)
$game = array(
$game_id => array(
'map' => array(
$place = array(...)
)
)
);
This should work

PHP object property notation

I suddenly stuck here:
$source = (object) array(
'field_phone' => array(
'und' => array(
'0' => array(
'value' => '000-555-55-55',
),
),
),
);
dsm($source);
$source_field = "field_phone['und'][0]['value']";
dsm($source->{$source_field}); //This notation doesn't work
dsm($source->field_phone['und'][0]['value']); //This does
dsm() is Drupal developer function for debug printing variables, objects and arrays.
Why $source object doesn't understand $obj->{$variable} notation?
Notice: Undefined property: stdClass::$field_phone['und']['0']['value']
Because your object does not have a property that is named "field_phone['und'][0]['value']". It has a property that is named "field_phone" which is an array which has an index named "und" which is an array which has an index 0 and so on. But the notation $obj->{$var} does not parse and recursively resolve the name, as it shouldn't. It just looks for the property of the given name on the given object, nothing more. It's not like copy and pasting source code in place of $var there.

Stop notices from undefined indexes in arrays

PHP returns a "notice" whenever an array index is undefined.
How can I turn these notices off? Or even better, correctly code for them?
Example:
$job_db_ready = array(
"email" => $this->profile['email'],
"company" => $job['company']['name'],
"position" => $job['title'],
"industry" => $job['company']['industry'],
"start_date_month" => $job['startDate']['month'],
"start_date_year" => $job['startDate']['year'],
"end_date_month" => $job['endDate']['month'], // Sometimes endDate undefined
"end_date_year" => $job['endDate']['year'], // Sometimes endDate undefined
"is_current" => $job['isCurrent']
);
This array will return
Severity: Notice
Message: Undefined index: endDate
1) Better Coding Style :Check if your variable is defined first with isset before assigning it to a key in the array. Like this
isset($job['endDate']['month']) // Do something about it
2) Turning of error: Use this in your script before doing something weird
error_reporting(0);
1 is recommended 2 is NOT
Turning of notices is a bad practice as they are there to alert you to possible errors on your part. Instead, check to see if they are defined and if they aren't assign a default value (including null or an empty string):
"end_date_month" => (isset($job['endDate']['month']) ? $job['endDate']['month'] : ''),
"end_date_year" => (isset($job['endDate']['year']) ? ($job['endDate']['year'] : ''),
Put the following at the top of your PHP script:
error_reporting( 0 );
See error_reporting on the PHP docs for further information.

Accessing data from CakePHP's find() return array

Here's an example of an array that is returned by CakePHP's find() method:
Array
(
[Tutor] => Array
(
[id] => 2
[PersonaId] => 1
)
)
The official documentation shows how to fetch records, but does not show how to iterate through them or even read out a single value. I'm kind of lost at this point. I'm trying to fetch the [id] value within the array. Here's what I've tried:
// $tutor is the array.
print_r($tutor[0]->id);
Notice (8): Undefined offset: 0
[APP\Controller\PersonasController.php, line 43]
Notice (8): Trying to
get property of non-object [APP\Controller\PersonasController.php,
line 43]
I've also tried:
// $tutor is the array.
print_r($tutor->id);
Notice (8): Trying to get property of non-object [APP\Controller\PersonasController.php, line 44]
The -> way of accessing properties is used in objects. What you have shown us is an array. In that example, accessing the id would require
$tutor['Tutor']['id']
Official PHP documentation, "Accessing array elements with square bracket syntax":
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]); //"bar"
var_dump($array[42]); //24
var_dump($array["multi"]["dimensional"]["array"]); //"foo"
?>
The returned value is an array, not an object. This should work:
echo $tutor['Tutor']['id'];
Or:
foreach($tutor as $tut){
echo $tut['Tutor']['id'] . '<br />';
}

Categories