PHP sin() function


The sin() function in PHP is used to calculate the sine of a given angle, which is expected to be in radians. The sine function is a fundamental trigonometric function that computes the ratio of the length of the side of a right-angled triangle opposite the angle to the length of the hypotenuse.

Syntax:

sin($angle_in_radians)

Parameters:

  • $angle_in_radians: The angle in radians for which you want to calculate the sine value.

Return Value:

  • The function returns the sine of the given angle as a float.

Important Note:

Since sin() expects the angle in radians, if you have the angle in degrees, you need to convert it to radians using the deg2rad() function.

Example 1: Calculating Sine of an Angle in Radians

<?php $angle_in_radians = pi() / 2; // pi/2 radians (90 degrees) // Calculate sine of the angle $sine_value = sin($angle_in_radians); echo $sine_value; ?>

Output:

1

In this example, sin(π/2)\sin(\pi/2) or sin(90)\sin(90^\circ) is 1.

Example 2: Calculating Sine of an Angle in Degrees

Since sin() expects radians, you need to use deg2rad() to convert an angle from degrees to radians.

<?php $angle_in_degrees = 30; // Convert degrees to radians $angle_in_radians = deg2rad($angle_in_degrees); // Calculate sine of the angle $sine_value = sin($angle_in_radians); echo $sine_value; ?>

Output:

0.5

In this case, sin(30)\sin(30^\circ) is 0.5.

Practical Usage:

  • The sin() function is widely used in geometry, physics, engineering, and anywhere trigonometric calculations are required.
  • It's particularly useful when dealing with periodic functions, oscillations, or when calculating the height or position in circular or wave-like motions.

Example 3: Using sin() for Simple Harmonic Motion

In physics, the position yy of an object in simple harmonic motion at time tt is given by the equation:

y=Asin(ωt)y = A \sin(\omega t)

Where:

  • AA is the amplitude,
  • ω\omega is the angular frequency,
  • tt is time.
<?php $A = 10; // Amplitude $omega = pi(); // Angular frequency (radians per second) $t = 2; // Time in seconds // Calculate position in simple harmonic motion $y = $A * sin($omega * $t); echo "Position at time t = 2s: " . $y; ?>

Output:

Position at time t = 2s: -10

Summary:

  • sin() in PHP calculates the sine of an angle in radians.
  • To use degrees, convert them to radians using deg2rad().
  • It is frequently used in trigonometric, geometric, and physics-related calculations.