CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Messing? PHP Code but it works...  (Read 276 times)

mmerlinn

  • Jr. Member
  • **
  • Posts: 53
  • Karma: +5/-0
  • Nutcake
Re: Messing? PHP Code but it works...
« Reply #30 on: 11 Jan 2021, 03:17:04 am »


My first personal computer was a 4k Tandy TRS80 with a tape-deck and saving bytes was mandatory to prevent out of memory errors.  Now I am more concerned with making my scripts easily readable because more time is spent trying to debug old code.

I am delighted that computers are so much faster and less expensive nowadays :)


Due to my experience with Applesoft, I tend to put everything in functions, debug them, then forget about them.  I am still using Foxpro functions every day that I wrote and debugged 25 years ago.
The soul purr pus of a spell cheque cur is two valley date hour ignore ants.

GrumpyYoungMan

  • Sr. Member
  • ****
  • Posts: 393
  • Karma: +4/-0
    • GrumpyYoungMan
Re: Messing? PHP Code but it works...
« Reply #31 on: 11 Jan 2021, 06:40:14 am »
I prefer using PHP functions to extract strings rather than REGEX. They may be a little more verbose but far easier to modify and update...


so I was procrastinating and making good with my time because I want to use some of the script on another project:


https://this-is-a-test-to-see-if-it-works.tk/codingforum/GrumpyYoungMan/registration-plates/
Code: [Select]
Warning: highlight_file(index-001.php): Failed to open stream: No such file or directory in SNIP on line 71

Warning: highlight_file(): Failed opening 'index-001.php' for highlighting in SNIP on line 71

Are the contents of these files important? ;)
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 650
  • Karma: +116/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Messing? PHP Code but it works...
« Reply #32 on: 11 Jan 2021, 08:45:40 am »
So is it just the normal with PHP - there is more than one way to do it, rather a practically bad way?
There are a lot of ways to do things. The trick isn't so much that any one way is "wrong" for all cases as finding the one that best fits the needs of the job.

Sometimes you want to sacrifice for speed, other times it's all about code clarity. Sometimes you have to worry about memory footprint, or disk footprint, or how much CPU you're hogging.

For example, I might rag on double quotes, but how else does one in PHP get escape sequences like \r\n\t? It's not "never use double quotes" it's "don't use them for everything."

To Regex or not is another example. Some people will say that starting up the regex engine and its overhead is "bad", but that can lead to false assumptions compared to the alternatives. Regex is implemented via optimized C code that is well, WELL proven. The parsing of the regex string is highly efficient, as is how it's rules are done via assembly. As such it can often be faster than anything you could write in the INTERPRETED PHP language. It comes down to how complex the process you're trying to implement is.

It's far too easy -- and I catch myself doing this too -- to try and find "one way of doing things". The problem is that all these different ways exist for a reason, and to reject them outright for one pedantic "ideal" or another is short-sighted and foolish.

You can see this in the inefficiencies of things like Java's "Everything has to be an object". It takes a good idea to an extreme, and it actually hampers the code clarity and efficiency of the language. On the polar opposite side you have "functional programming" which is so knee deep in career educator pedantry and bald faced lies, that its adherents spend more time packing their own fudge than they do making useful code.

Its fanboys have actually convinced themselves that several dozen lines of callbacks and stack passing hell is more efficient than a simple one line "for" loop... all because of claims of "For loops" being a "bad way" of doing things.

It's a far too easy a pit to fall into. You'll get in your head a great sounding ideal, and it screws you over without you even realizing it. Quite often these "ideals" are highly popular, in fact I'd go so far as to say the more popular it is, the more likely it is to bend you over the table. Remember, popular doesn't necessarily make something good, or right, or just. See the crime against music that is Billie Eilish's Grammy, or all the nose-breathing idiots who pander to the first demagogue to cross their paths.



Sorrow hides well in your shell. A fellow man with hurt to spare.
Dear one, here I am to share the fear. An act of kindness, without an amen.
Come in, the fire's warm. Burn the rope and dance some more.

John_Betong

  • Full Member
  • ***
  • Posts: 135
  • Karma: +10/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #33 on: 12 Jan 2021, 02:08:47 am »
Quote
@GrumpYoungMan,  Are the contents of these files important? 

The index-001.php is the PHP main source file which uses highlight_file(...); to display the contents so that anyone interested can copy and paste locally to try the application on your localhost. Please note that the data.php file contents also requires downloading into the same directory.

I have modified the PHP source files and added a couple more sample registrations.

Please let me know if the registrations are all valid. If not then registration and details of why it is invalid.
« Last Edit: 12 Jan 2021, 02:11:41 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Sr. Member
  • ****
  • Posts: 393
  • Karma: +4/-0
    • GrumpyYoungMan
Re: Messing? PHP Code but it works...
« Reply #34 on: 12 Jan 2021, 02:30:22 am »
Quote
@GrumpYoungMan,  Are the contents of these files important? 

The index-001.php is the PHP main source file which uses highlight_file(...); to display the contents so that anyone interested can copy and paste locally to try the application on your localhost. Please note that the data.php file contents also requires downloading into the same directory.

I have modified the PHP source files and added a couple more sample registrations.

Please let me know if the registrations are all valid. If not then registration and details of why it is invalid.
Thanks, I give it a look at once I get to the workshop!

I manage to get it working with the data.php.... so I didn’t think the missing files were anything I needed!

Thanks again! I appreciate it!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Sr. Member
  • ****
  • Posts: 393
  • Karma: +4/-0
    • GrumpyYoungMan
Re: Messing? PHP Code but it works...
« Reply #35 on: 12 Jan 2021, 04:13:38 am »
Please let me know if the registrations are all valid. If not then registration and details of why it is invalid.

Based on this:
Number(s) 1, through to 7, 10, 15, 16, 17, 18 are also ALL valid...

1-7 are the old pre 2001 style...

13 and 14 is the new post 2001 style and they are being predicted correctly!

the rest are all classed as personalised and they could go down to a single number and letter. ;)

Sorry....

Is this helpful?

« Last Edit: 12 Jan 2021, 04:16:52 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Sr. Member
  • ****
  • Posts: 393
  • Karma: +4/-0
    • GrumpyYoungMan
Re: Messing? PHP Code but it works...
« Reply #36 on: 12 Jan 2021, 04:21:37 am »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 135
  • Karma: +10/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #37 on: 13 Jan 2021, 12:16:34 am »
Hi @GrumpyYoungMan,

It appears to me from post #35 and #36 that there are three essential requirements:

1. first character must alphabetical
2. cannot use characters  I, O or Z
2. at least one other character must be numeric

Validation check:
Code: [Select]
//=========================================
//  Valid if and only if
//  1. first character is a character
//  2. CANNOT START WITH I, O or Z
//  3. at least one following character is a number
//=========================================
public function getV4(string $vrm)
: string
{
   $result = ''; // FALSE

   # IS THE FIRST CHARACTER A CHARACTER
   if( strlen($vrm) > 1 && ctype_alpha($vrm[0]) ) :
       # CANNOT HAVE O or Z
         if(   strpos('?' .$vrm, 'I')
                  || strpos('?' .$vrm, 'O')
                  || strpos('?' .$vrm, 'Z')
         ):
             // FALSE
         else:   
            # IS THERE A NUMBER IN THE REMAINING STRING
          for($i2=1; $i2<strlen($vrm); $i2++):
                if( is_numeric($vrm[$i2]) ) :
                    $result = $vrm;
                    break;
                endif;   
          endfor;     
      endif;   
   endif;
       
   return $result;
}//

Online application updated and source available.

Please note that if $vrm is user input then a validation check to ensure only alphanumeric characters are accepted - I will leave that up to you :)
« Last Edit: 13 Jan 2021, 12:54:01 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Sr. Member
  • ****
  • Posts: 393
  • Karma: +4/-0
    • GrumpyYoungMan
Re: Messy? PHP Code but it works...
« Reply #38 on: 13 Jan 2021, 04:40:42 am »
Hi @GrumpyYoungMan,

It appears to me from post #35 and #36 that there are three essential requirements:

1. first character must alphabetical
2. cannot use characters  I, O or Z
2. at least one other character must be numeric

Validation check:
Code: [Select]
//=========================================
//  Valid if and only if
//  1. first character is a character
//  2. CANNOT START WITH I, O or Z
//  3. at least one following character is a number
//=========================================
public function getV4(string $vrm)
: string
{
   $result = ''; // FALSE

   # IS THE FIRST CHARACTER A CHARACTER
   if( strlen($vrm) > 1 && ctype_alpha($vrm[0]) ) :
       # CANNOT HAVE O or Z
         if(   strpos('?' .$vrm, 'I')
                  || strpos('?' .$vrm, 'O')
                  || strpos('?' .$vrm, 'Z')
         ):
             // FALSE
         else:   
            # IS THERE A NUMBER IN THE REMAINING STRING
          for($i2=1; $i2<strlen($vrm); $i2++):
                if( is_numeric($vrm[$i2]) ) :
                    $result = $vrm;
                    break;
                endif;   
          endfor;     
      endif;   
   endif;
       
   return $result;
}//

Online application updated and source available.

Please note that if $vrm is user input then a validation check to ensure only alphanumeric characters are accepted - I will leave that up to you :)
Thanks - Appreciate it!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

 

SMF spam blocked by CleanTalk

Advertisement