1

I would like to do something quite simple but I dont know the right code in php.

I have a variable $go which content is GO:xxxxx

I would like to query that if the content of a variable has the pattern "GO:" and something else, echo something, but if not, echo another thing

I want to declare an if statement like:

if (preg_match('/GO/', $go) {
echo "something";
}
else {
echo "another thing";

But I cannot make it work...

I want to embed this statement between this portion of my script:

$result = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where go_id like '%" . $go . "%'");

$items = mysqli_fetch_assoc($result);

echo "<br/>";

echo "<b> The total number of genes according to GO code is:</b> " . $items['total'];

mysqli_free_result($result);

$result2 = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where db_object_name like '%" . $go . "%'");

$items2 = mysqli_fetch_assoc($result2);

echo "<br/>";

echo "<b>The total number of genes according to GO association is:</b> " . $items2['total'];

mysqli_free_result($result2);

As it is right now, the variable $go can have a value like GO:xxxx or a random sentence, and, with this code, I get two strings, one with value 0 and another with value according to the total apperances matching $go content.

What I want is to declare an if statement so that it just prints one string, the one that has the number of matches according to $go content, but not both.

Any help?

  • Possible duplicate of [startsWith() and endsWith() functions in PHP](http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php) – Yoshi Dec 15 '16 at 11:10

2 Answers2

3

Use strpos():

if (strpos($go, 'GO:') !== false) {
    echo 'true';
}
deChristo
  • 1,860
  • 2
  • 17
  • 29
1

Try this

echo (!strpos($go, 'GO:')) ? "another thing" : "something";

Will surely work

Sunny Kumar
  • 823
  • 6
  • 14