K
K
Kirill Gorelov2015-09-20 22:25:19
PHP
Kirill Gorelov, 2015-09-20 22:25:19

How to make the output more correct in php?

Hello.
A question.
I am doing etude programming in php. That is, I just want to learn the language for myself. Now I am engaged in input and output of information.
And so.
The code.
-------------------------------------------------- ------------
<?php include 'config.php'; ?>
<?php include 'dbconfig.php'; ?>
"/>
......
--------------------------------------- --------------------
I display the description in two ways: By the
contents of the files
1st file
<?PHP
define ("description", "description1");
?>
2nd file.
<?PHP
$config = array (
'description' =>
?>
The question itself.
To what extent is it reasonable, correct, etc. the use of such constructs in programming. As for the output information itself and the contents of the files?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2015-09-20
@xmoonlight

an array is better than a constant - it can be manipulated.
constant - usually for paths or some values ​​that should not change inside the script.

N
newpy, 2015-09-21
@newpy

In the context in which you use in the example, i.e. description or "description", it may not be entirely appropriate to set a constant.
A simple example:
appropriate:
here you set a constant for the number Pi, for example, up to the sign you need. This is immutable data. A good reason to be constant.
appropriate:
in this case, it is also appropriate, because M52B20 engines from BMW have a volume of 2 liters, they left the assembly line with just such a code and such a volume. And nothing will change that. Convenient to use as a constant for quick access.
Continuing the topic of cars, now an example with an array:

$bmwE36 = array (
'year' => '1996',
'color' => 'deepblue',
'engine' => BMW_M52B20_ENGINE,
'type' => 'sedan',
);

In this case, we describe the car and set it using an array, the car has a set of properties, and the cars were produced differently, they could have a different color, year of manufacture, type (station wagon, sedan, hatback), engine.
In this case, I just set the engine with a constant from the example above. It would be logical, for example, for car types (station wagon, sedan, hatchback and coupe), for example, to also set constants. Because it's persistent data, and there aren't many of them. Suppose I gave three - accordingly, it would be possible to set three constants and and set the number of doors to them. A sedan usually has 4 doors, a coupe has 2 (or 3), and a station wagon usually has 5.
define ("SEDAN", 4);
define ("CUPE", 3);
define ("UNIVERSAL", 5);

then our array can also be set like this:
$bmwE36 = array (
'year' => '1996',
'color' => 'deepblue',
'engine' => BMW_M52B20_ENGINE,
'type' => SEDAN,
);

If you wish, you can change the properties of the car by simply accessing the elements of the array. You can sort the properties, you can select only the ones you need and then use them somehow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question