Jump to content

Recommended Posts

I have such lines of code:

$link["beF1LP"]="https://www.site.com";
$link["beN1LP"]="https://www.site.com";

I know I can simplify it like this:

$link["beF1LP"]=$link["beN1LP"]="https://www.site.com";

But I wanted something like this:

$link["be(F|N)1LP"]="https://www.site.com";

Which did not work.

How do I get it working for the string inside the brackets and under the quotes while using OR and/or other operators?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/313032-making-code-shorter/
Share on other sites

  • 4 months later...

One more question on this. If I have multiple lines of $link variable, can I use "for each" statement only once, and then list my $link variables under, in some sort of brackets? This is what I would have now:

foreach(['F','N'] as $x) $link["be{$x}1LP"]="https://www.site1.com";

foreach(['F','N'] as $x) $link["be{$x}2LP"]="https://www.site2.com";

foreach(['F','N'] as $x) $link["be{$x}3LP"]="https://www.site3.com";

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591765
Share on other sites

Your 3 arrays:

$link = array_merge(
    array_fill_keys(['beF1LP','beN1LP'], 'https://www.site1.com'),
    array_fill_keys(['beF2LP','beN2LP'], 'https://www.site2.com'),
    array_fill_keys(['beF3LP','beN3LP'], 'https://www.site3.com')
);

 

Link to comment
https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591767
Share on other sites

15 hours ago, phpnew said:
$item = array('L','N');
foreach ($item as $x) { 
$link["{$x}1c2"]=$link["{$x}1c2CP"]=site1.com;
$link["{$x}1c3"]=$link["{$x}1c3CP"]=site2.com;}

Just before closing the book on this one, please ask yourself this:

In [another] four months time, are you going to look at this code and ask yourself "What the H*** does this do?"

You will spend far more time reading code than writing it (accepted industry stats estimate 80% reading, 20% writing). 

Always favour Clarity and Correctness over Conciseness or Cleverness. 

Regards, 
   Phill  W.

 

  • Like 2
Link to comment
https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591772
Share on other sites

+1 on PHi11W's comment.

While it was an interesting exercise, I was thinking that I wouldn't want to have to pick up that code later and extend it.   A few obscure lines that do what a simple assignment statement could do, is not better code.

Link to comment
https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591787
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.