phpnew Posted July 2, 2021 Share Posted July 2, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/ Share on other sites More sharing options...
Barand Posted July 2, 2021 Share Posted July 2, 2021 Not shorter with only two links, but less repetition foreach(['F','N'] as $x) $link["be{$x}1LP"]="https://www.site.com"; Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1587765 Share on other sites More sharing options...
requinix Posted July 2, 2021 Share Posted July 2, 2021 🏌️⛳ Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1587769 Share on other sites More sharing options...
phpnew Posted July 4, 2021 Author Share Posted July 4, 2021 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1587826 Share on other sites More sharing options...
phpnew Posted November 4, 2021 Author Share Posted November 4, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591765 Share on other sites More sharing options...
gizmola Posted November 4, 2021 Share Posted November 4, 2021 Ok I'll play: $link = array_fill_keys(['beF1LP','beN1LP'], 'https://www.site.com'); Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591766 Share on other sites More sharing options...
gizmola Posted November 4, 2021 Share Posted November 4, 2021 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') ); Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591767 Share on other sites More sharing options...
phpnew Posted November 4, 2021 Author Share Posted November 4, 2021 Got it! $item = array('L','N'); foreach ($item as $x) { $link["{$x}1c2"]=$link["{$x}1c2CP"]=site1.com; $link["{$x}1c3"]=$link["{$x}1c3CP"]=site2.com;} Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591768 Share on other sites More sharing options...
Phi11W Posted November 5, 2021 Share Posted November 5, 2021 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. 2 Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591772 Share on other sites More sharing options...
gizmola Posted November 5, 2021 Share Posted November 5, 2021 +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. Quote Link to comment https://forums.phpfreaks.com/topic/313032-making-code-shorter/#findComment-1591787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.