Home > database >  How do I set a variable then call the updated variable in the same function? PHP
How do I set a variable then call the updated variable in the same function? PHP

Time:04-14

I am trying to create a function where I check whether the label has been set. Update the label based on whether it is set or not, then call that updated label in another variable.

function update_label() {
  if (empty($label) {
    $label = 'foo';
  } else {
    $label = $label . ' bar';
  }
  $article_meta = $label . ' updated' . time();
}

I am checking if the label has been set if not to Populate label as foo. If a label has been set to update with bar after the label. These 2 work. But then I am trying to update a third field which contains the updated field but it always passes on the original value.

So if the label has been set $article_meta should return 'label bar updated today' but returns 'label updated today'.

Any ideas?

Thanks

CodePudding user response:

This should be the easy fix

function update_label(string $label): string {
  if (empty($label)) {
    $label = 'foo';
  } else {
    $label = $label . ' bar';
  }
  return $label . ' updated' . time();
}
  •  Tags:  
  • php
  • Related