Mapping groups of constant values [duplicate] - php

This question already has answers here:
Enumerations on PHP
(39 answers)
Closed 2 years ago.
I have lists where strings correspond with values. For example, in a company list I could have Microsoft correspond with the letters MS.
Obviously, this could be transposed as a table in MySQL to make the list extensible but, from curiosity, how would you express this in constant form in PHP ?
P.S: There is the class-with-constants approach (see accepted answer here: PHP and Enumerations) to act as an enumeration but would that really be of any use seeing that enumerations map to integer values ?

How about using define
define("MS","Microsoft");
echo MS;
This would echo Microsoft.
http://php.net/manual/en/function.define.php
Also the link you gave to a possible solution could just as easily be used with strings instead. The only reason it acts as an enum from other languages is because you define the values from 0 to n, instead of doing that just use string instead.
class Companies {
const MS = 'Microsoft';
const IBM = 'International Business Machines';
}
echo Companies::MS;
I think this would work.

i would probably start by looking at multi dimentional arrays if a single company can have many corresponding letters, however the draw back being that they can get difficult to manage, your other option is to define constants

If you have key-value pairs you can put those into an object, so its more practical to use.
check on this link: the arrayToObject() function

Related

How to solve a formula from a mysql database field in php?

In my frontend I have someone who can change a formula in a form field.
The formula will be saved in a mysql database (text field right now).
For example this: "4 + (5 + 2 ) ^ (6 * 10)".
Now I want to solve this in my php code. My Problem is there I need the function
"pow" for example.
Is there any good solution to handle all or most math functions for php. Or do I have to manipulate the "^" to pow(4 + (5 + 2), (6 * 10)).
There are at least two solutions: either use the eval function which I don't recommend for security reasons (easiest way to achieve your goal), or use any expression library (like Symfony's Expression Language).
While the former would be the fastest way, I would recommend the latter. There's a lot of flexibility you can achieve by exposing functions you only need or writing custom ones.

how is youtube url constructed? [duplicate]

This question already has answers here:
YouTube URL algorithm?
(11 answers)
Closed 3 years ago.
I am wondering how the youtube url is constructed? for example https://www.youtube.com/watch?v=L7Jd910Bw84
how is the value L7Jd910Bw8 created, is it a hash value ?
is there any PHP function to use for creating this kind of urls?
how is it possible to make sure that the two diffrent objects dont get the same value ?
what is the advantages and disanvatages using this kind of url?
Thanks :) !
It is basically just an ID to uniquely identify a video. Technically, there is no need to hash it, but they do encode it into a string to make the ID a little more unreadable.
Not specifically for YouTube URLs, but it is easy to create your own "hashed" ID string for any resource item with a numeric ID (like an auto-increment numeric primary key). You just run the numeric ID through some simple reversible math function, then encode the resultant number with an alphabet of characters you want, e.g. base58. Or you can simply hash it with a more complicated algorithm, if CPU power is not a concern.
If your IDs are different to begin with, there should be no collisions if all you do is a simple encoding, e.g. some math function, then decimal to base58. Even if you use a more complicated algorithm, there's also next-to-zero chance for most algorithms.
Advantage and Disadvantage
Advantage: security / privacy. Since most numeric IDs run in sequence, if you simply use id=123456, everyone sort of knows that you have 123456 items in your DB. With an encoded string, nobody knows.
Disadvantage: processing. Obviously, there's going to be a slight increase in processing required to generate these IDs.

Column Named Order - Big Database, How Should I Go About This? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Are there any reserved words in SQLite?
I have a big database (veekun's pokedex for those who know of it) and one table has a column named "order". It's for sorting purposes, and I'm trying to ORDER BY that column, but it is a problem since ORDER is, of course, a reserved word. The two options I see are
Rename this column (but I'd like to avoid modifying the database as much as possible)
Get around this somehow
So, in the case of 1, how would I go about renaming the column without messing up the database too much?
In the case of 2, I can't imagine what I could do, but maybe there's a trick I don't know of to get around this.
I'm using SQLite and PHP.
you just need to surround keyword collisions in `backticks`
Some databases support ` as escape character, others use […]. Looking up SQLlite's, it supports brackets:
order by [Order]
should work.

PHP string length [duplicate]

This question already has answers here:
PHP String Length Without strlen()
(3 answers)
Closed 3 years ago.
PHP coding standards say:
... PHP holds the length property of each string, and that it
shouldn't be calculated with strlen(). Write your functions in a such
a way so that they'll take advantage of the length property, both
for efficiency and in order for them to be binary-safe. ...
How can I access this length property? Or do I misunderstand it?
As Ignacio mentioned in another post:
They're talking about the C function, not the PHP function. The C
function will stop counting after the first \0, but PHP strings can
contain \0 elsewhere other than the end.
That document is most likely about how to extend PHP's engine, rather than programming in PHP itself.
These coding standards are for not intended for web sites developpers using PHP, but for the developpers of the PHP engine itself.
PHP is developped using the C language, and the strlen function to avoid is the C one, not the PHP one.
It's not a "property" in the sense that it would be in other languages (like C#).
You cannot do:
myString.Length;
Instead you would need to do:
strlen(myString);

Construct a Magic Square by a given number of rows [duplicate]

This question already has answers here:
How to create a magic square in PHP?
(3 answers)
Closed 6 years ago.
I'm trying to build an application in simple PHP but could be done in any language.
I want the user to enter number of rows and the output will be the magic square of it.
For example, for $rows=3:
8-1-6
3-5-7
4-9-2
Notice that in every row,column,diagonal the sum of numbers is equal to 15.
There is a method for constructing this with an odd number of rows. I DONT want that. I want the program to ACTUALLY calculate numbers, row-sums, column-sums and diagonal-sums, doesn't matter if the input is odd or even.
Do you have any idea how to go about doing this? Right now i'm drowning in a sea of loops.
HELP!
Sorry, after a further search I found a JAVA applet that does it:
link text
It's pretty sweet thinking about the extensive mechanism working behind this seemingly simple algorithm.

Categories