# Can someone tell me what this line does

**URL:** <https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822>\
**Category:** Code Snippets\
**Created:** [August 15, 2021, 12:59am UTC](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822 "2021-08-15T00:59:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnselTk1](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@AnselTk1](https://www.phphelp.com/u/AnselTk1)\
**Post date:** [August 15, 2021, 12:59am UTC](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822/1 "2021-08-15T00:59:26Z")

</div>

Hi - working on moving some existing code in a site. Please forgive the PHP newbie question. I have this function:

```auto
function cc_remove_cpt_slug( $post_link, $post, $leavename ) {
  if(in_array($post->post_type, array('toronto','calgary','edmonton','halifax')) ) {
    $post_type = $post->post_type == 'toronto' ? 'toronto' : $post->post_type;
    $post_link = str_replace( '/' . $post_type . '/', '/', $post_link );
    return $post_link;
  } else {
    return $post_link;
  }
}
add_filter( 'post_type_link', 'cc_remove_cpt_slug', 10, 3 );
```

Can someone please explain what line 3 does?

$post\_type = $post-\>post\_type == ‘toronto’ ? ‘toronto’ : $post-\>post\_type;

Thanks

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [August 15, 2021, 5:22am UTC](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822/2 "2021-08-15T05:22:33Z")

</div>

$post is an OBJECT. $post-\>post\_type is a part of the object.  
The second line checks if the post\_type is in the array of the four cities.  
The third line sets the post\_type in the $post OBJECT to ‘toronto’ OR leaves it set to the current value.  
It is basically a shorthand version of a full compare. The following two examples are exactly the same…

```auto
$post_type = ( $post->post_type == 'toronto' ) ? 'toronto' : $post->post_type;

if ( $post->post_type == 'toronto' ) {
   $post->post_type = 'toronto';
} else {
   $post->post_type = $post->post_type;
}

```

\*\*\* Note, this is very odd code. It basically checks if checks for four different cities using an array. Then, if it does exist in the array, it changes the city to either ‘toronto’ or whatever was in it to start. I think it is trying to remove all the other 3 from the list. It basically makes the result ‘toronto’ if it is any of the four cities. For some reason the previous programmer wanted those four cities to be lumped into just the Toronto city. Hope this helps…

---

<div class="post-metadata">

**Author:** ![jpajak](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jpajak](https://www.phphelp.com/u/jpajak)\
**Post date:** [August 15, 2021, 9:41am UTC](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822/3 "2021-08-15T09:41:42Z")

</div>

The line below:

`$post_type = $post->post_type == ‘toronto’ ? ‘toronto’ : $post->post_type;`

checks: if the class variable “post\_type” has the value “toronto”, then it assigns “toronto” to the $post\_type variable. Otherwise it takes whatever is in the class variable.

This check seems to be redundant and if anything, seems like a leftover from code refactorization and developer laziness. It could easily be shortened simply to:

`$post_type = $post->post_type`

Or even remove the `$post_type` variable completely and just end with:

`str_replace( '/' . $post->post_type . '/', '/', $post_link );`

---

<div class="post-metadata">

**Author:** ![AnselTk1](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@AnselTk1](https://www.phphelp.com/u/AnselTk1)\
**Post date:** [August 15, 2021, 7:29pm UTC](https://www.phphelp.com/t/can-someone-tell-me-what-this-line-does/33822/4 "2021-08-15T19:29:17Z")

</div>

Thank you both for your help!
