13 Replies - 788 Views - Last Post: 05 October 2020 - 08:52 PM Rate Topic: -----

#1 vipery2kx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-May 19

The GOTO Operator

Posted 04 October 2020 - 07:28 AM

The PHP manual doesn't give much info on GOTO, but does anyone know if I can "goto" a section of the code above the declaration?

I'm generating a UUID/GUID with PHP, and since they are suppose to be unique I would like to also check if the UUID already exists.
[Pseudocode] A way I'm thinking about implementing this is

regenuuid:

"SELECT `uuid` FROM `user` WHERE `uuid`=?
if(rowCount() == 1)
  goto regenuuid;



I've read up on how UUID() is generated, and the possibility of getting a duplicate value is very slim. But I would like to be prepared instead of displaying a message like "We are unable to create your account right now".

Is This A Good Question/Topic? 0
  • +

Replies To: The GOTO Operator

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3178
  • View blog
  • Posts: 12,004
  • Joined: 03-December 12

Re: The GOTO Operator

Posted 04 October 2020 - 07:47 AM

Oh my god no, learn how to use functions properly!!!!
Was This Post Helpful? 4
  • +
  • -

#3 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3178
  • View blog
  • Posts: 12,004
  • Joined: 03-December 12

Re: The GOTO Operator

Posted 04 October 2020 - 10:41 AM

FYI not sounding like a “douche”, goto should not be used in a high level language. You are doing this wrong, it isn’t my fault you don’t like to hear it.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12076
  • View blog
  • Posts: 20,477
  • Joined: 19-March 11

Re: The GOTO Operator

Posted 04 October 2020 - 11:06 AM

I agree, the most fundamental purpose of using programming languages is to provide structure to code in various forms. All of these structures have one thing in common, and that is to obviate the need for an unrestricted branch operation.
Was This Post Helpful? 1
  • +
  • -

#5 rouncer   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-May 18

Re: The GOTO Operator

Posted 04 October 2020 - 12:15 PM

Maybe it helps with code obsfuscation!

Its terrible code, but they come in handy in while and for loops for aborts. :)

This post has been edited by rouncer: 04 October 2020 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12076
  • View blog
  • Posts: 20,477
  • Joined: 19-March 11

Re: The GOTO Operator

Posted 04 October 2020 - 12:59 PM

View Postrouncer, on 04 October 2020 - 02:15 PM, said:

Maybe it helps with code obsfuscation!



Well, sure, it can certainly help with that, but you don't even really need a GOTO for that. Look at perl :)
Was This Post Helpful? 1
  • +
  • -

#7 Ornstein   User is offline

  • D.I.C Regular

Reputation: 118
  • View blog
  • Posts: 273
  • Joined: 13-May 15

Re: The GOTO Operator

Posted 05 October 2020 - 05:22 AM

View Postrouncer, on 04 October 2020 - 12:15 PM, said:

Its terrible code, but they come in handy in while and for loops for aborts. :)


Out of interest, what sort of situations have you found yourself needing goto instead of break or continue?
Was This Post Helpful? 1
  • +
  • -

#8 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3178
  • View blog
  • Posts: 12,004
  • Joined: 03-December 12

Re: The GOTO Operator

Posted 05 October 2020 - 08:31 AM

<?php

$uuids = [];
for($i=0; $i < 10000; $i++){
  $uuid = uniqid();
  if(!in_array($uuid, $uuids))
	  array_push($uuids, $uuid);
}

// generate UUID that is not currently in the database
function generateId($uuids) {
	
//	$stmt = $this->_pdo->prepare("SELECT COUNT(`uuid`) FROM `user` WHERE `uuid`=?");
	do {
		$uuid = uniqid();
//		$stmt->execute([$uuid]);
//		$result = $stmt->fetchColumn();
	} while (in_array($uuid, $uuids));
  echo "Found a new one!";
	return $uuid;
}
echo generateId($uuids);

Was This Post Helpful? 0
  • +
  • -

#9 rouncer   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-May 18

Re: The GOTO Operator

Posted 05 October 2020 - 02:00 PM

View PostOrnstein, on 05 October 2020 - 05:22 AM, said:

View Postrouncer, on 04 October 2020 - 12:15 PM, said:

Its terrible code, but they come in handy in while and for loops for aborts. :)/>/>


Out of interest, what sort of situations have you found yourself needing goto instead of break or continue?


I started off in basic, so every now and again I get this urge to put a goto in, if I get myself stuck, I havent done it in a while, and possibly I do have a goto in some code somewhere, but like ppl say its 100% avoidable, so cant quite remember when I did it last.

It goes something like this.

for(i=0;i<10;i++)
{
restartloop:
 //some rubbish here
 
 //then this wierd line happens at the end.

 if(some condition) {i++;goto restartloop}
}




I wish I had when I actually did it, but I cant remember...

This post has been edited by CTphpnwb: 05 October 2020 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3844
  • View blog
  • Posts: 14,019
  • Joined: 08-August 08

Re: The GOTO Operator

Posted 05 October 2020 - 03:04 PM

I don't see a need for goto there.
<?php
  $x = 0;
  for($i = 0; $i < 10; $i++) {
    echo $i."\n";
    if($x == 0 && $i ==  5){
      $x = 1;
      $i--;
      echo "Jumped back\n";
      continue;
    }
}


Output:

0
1
2
3
4
5
Jumped back
5
6
7
8
9
Was This Post Helpful? 1
  • +
  • -

#11 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6892
  • View blog
  • Posts: 31,732
  • Joined: 10-May 07

Re: The GOTO Operator

Posted 05 October 2020 - 04:27 PM

If you want the ID to be unique, why not use the primary key incriminator automatically? It's guaranteed to be unique because it's next.

Additionally I'll say that if I'm creating my account for a website, & the error message tells me that I need to try again later, chances are I'm not coming back.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12076
  • View blog
  • Posts: 20,477
  • Joined: 19-March 11

Re: The GOTO Operator

Posted 05 October 2020 - 05:50 PM

FWIW, since I do feel that the OP is owed an answer, even though we might think the effort is misguided, this link suggests that jump labels are legitimate as long as they are within the same scope. The main limitation mentioned is that it is not legitimate to jump into or out of a function (mercifully!)

So, yes, you should be able to jump backwards using a GOTO.

Still not a beautiful idea, of course, but it's your sanity, you can squander it any way you like.
Was This Post Helpful? 2
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3844
  • View blog
  • Posts: 14,019
  • Joined: 08-August 08

Re: The GOTO Operator

Posted 05 October 2020 - 08:29 PM

View Postjon.kiparsky, on 05 October 2020 - 07:50 PM, said:

Still not a beautiful idea, of course, but it's your sanity, you can squander it any way you like.

This assumes that no other developer will need to maintain the code at any point. The more I code, the more I believe that the clever or highly optimized way of doing things is likely to be the worst way to do them. GOTO might make a task easier in the short run, but later it's going to be a problem.

In the end, using GOTO is like not wearing a mask. It's fine if you live alone and work from home, never coming into contact with others, but in the real world your choice can have serious repercussions for those around you. Don't infect others with spaghetti code.
Was This Post Helpful? 1
  • +
  • -

#14 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12076
  • View blog
  • Posts: 20,477
  • Joined: 19-March 11

Re: The GOTO Operator

Posted 05 October 2020 - 08:52 PM

Obviously, I completely agree that using a GOTO is an idiotic thing to do, and I think I've made that pretty clear.

Quote

This assumes that no other developer will need to maintain the code at any point.


Yeah, I'm not too concerned no that front. I'm pretty sure this code will never be seen by any other developer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1